How can I change time format of TimePickerDialog dynamically? - java

I have a button, on click on the which the time format of a TimePickerDialog should change from 12 hours to 24 hours and vice versa.
I know I have to use onPrepareDialog(), but how can I change the time format inside onPrepareDialog ?
Any help is appreciated!
Thank you.

Finally did a work around to achieve this!.
Instead of using onPrepareDialog(),
I used a variable to keep tack of whether time should be shown in 24hrs or 12hrs format.
Boolean is24HoursSet;
which will be set to true when 24 hrs is selected by user, else false.
I added 2 constants
TIME_IN_12_HOURS //and
TIME_IN_24_HOURS
both for 12 hours and 24 hours format.
In touch listener, it calls showDialog().
//checks the variable, and calls according showdialog.
if(is24HoursSet)
{
showDialog(START_TIME_24);
}
else
{
showDialog(START_TIME_12);
}
which are handled in switch cases of onCreateDialog() by creating new object for
TimePickerDialog, with true or false as the last parameter of constructor which opens the timpicker with corresponding time formats. :)

Is it what you are looking for:
setIs24HourView(Boolean is24HourView);
Documentation

Related

How to indicate some fields are not supported

Trying to move from Calendar to the new Java 8 time on Android. Is there a way to indicate that a time or date field is not supported? I can use the 'Truncate' method that will set all time fields of a shorter duration to zero, so a time stamp like 2020-09-30T10:37:15.345-04:00 can be truncated say at the minutes level. But that will leave 2020-09-30T10:00:00.00-04:00.
However, what I want to indicate is that the clock does not have minutes or less precision so that when one tries to read the minutes or seconds, there will be some indication that there are no such fields or that they are unknown. Zero is a valid value.
Right now in the Calendar case I have to add numerous methods to a class to indicate that. For example, I made a class called TimeStruct which wraps a Calendar. If I take a time stamp like 2020-10-01T04:55 it does not have minutes. So to keep that information I have a variable 'isSecondsSet' and set it to false. I create the Calendar from the elements I DO have. But as soon as I call something like Calendar.getTimeInMillis() the seconds and milliseconds fields get set to 0 and are indicated as set. So my additional variables let me know that there was no seconds field.
I was hoping that the new classes would no longer require me to keep my own indicators and I would also be able to parse something like 2020-10-01T04:55. I could not, but I could parse a full time stamp. So if I do that and truncate, can I indicate that the truncated fields are not supported? That way I wont use a value of 0 in the seconds.

Compare two times in Android

I know this is a quite discussed topic but I think I have a very specific problem.
I'm storing opening and closing time of stores on a database as well as GPS coordinates. I'd like to be able to show the stores on a map with a green marker if the store is open and red if close (wrt current time).
My problem is that I'm using the method string.compareTo(string) to know if the store is closed or open. Let's say we are on monday, the store close at 02:00 in the morning (tuesday morning), current time is 23:00. How can I tell the store is still open since 02:00 < 23:00 ?
Thank you for any answer,
Arnaud
edit: I'm editing my answer to give a better idea of what I meant. Also, try to back up a second and re-think the way you store your data structure. This is a very important part of programming and in most cases can be the difference between a bad design and a good design implementation.
Storing the time as a string is not a good idea, but in your case what you should do (I think) is this: (This code assumes that the hours for the store opening and closing time refer to the same day as now)
// Get the current time.
Calendar now = Calendar.getInstance();
// Create the opening time.
Calendar openingTime = Calendar.getInstance();
// Set the opening hours. (IMPORTANT: It will be very useful to know the day also).
openingTime.set(Calendar.HOUR_OF_DAY, storeOpeningTime); // (storeOpeningTime is in 24-hours format so for 10PM use 22).
// Check that the store "was opened" today.
if (now.before(openingTime)) {
return CLOSED;
}
// If the store "was opened" today check that it's not closed already ("tricky" part).
// Create the closing time and closing time for the store.
Calendar closingTime = Calendar.getInstance();
// Check if we are in the AM part.
if (storeClosingTime < 12 // Closing time is in the AM part.
&& storeClosingTime < openingTime // Closing time is before opening time, meaning next day.
// now and closingTime is the same day (edge case for if we passed midnight when getting closingTime)
&& closingTime.get(Calendar.DAY_OF_WEEK) == now.get(Calendar.DAY_OF_WEEK)) {
// Closing time is next day.
closingTime.add(Calendar.DAY_OF_WEEK, 1);
}
// Set the closing hours.
closingTime.set(Calendar.HOUR_OF_DAY, storeClosingTime); // (storeClosingTime is in 24-hours format so for 10PM use 22).
// Check if the store is not closed yet.
if (now.before(closingTime)) {
return OPEN;
}
// Store is closed.
return CLOSED;
I haven't tested this, but I think according to your questions and how to save the opening and closing times, this should work.
Important: There are more edge cases and tweaks you can make to the code to improve it, but I don't have enough time to do it now. I wanted to give a general guiding hand to solve your problem. Handling time and dates is never fun, you need to keep in mind a lot of elements like time zones and clock shifting in winter and summer in different countries. This is not a finished implementation in any way, and getting it to a finished state is not simple.

How to set Chronometer in milliseconds (MM:SS:mm)

I have a chronometer used as a timer in a game. Currently it only shows seconds (by default). I have been trying to get the format to show in minutes:seconds:milliseconds. I tried but nothing is working. Here is the code I found on StackoverFlow that says it should work...but didn't. OR if you have any other solutions instead of chronometer please let me know! (This is in android, using java)
-Thanks
Chronometer chronometer;
chronometer.setFormat(MM:SS:mm);
Actually, there's a much nicer way of doing this:
void OnGUI() {
int minutes = Mathf.FloorToInt(timer / 60F);
int seconds = Mathf.FloorToInt(timer - minutes * 60);
string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
GUI.Label(new Rect(10,10,250,100), niceTime);
}
This will give you times in the 0:00 format. If you'd rather have 00:00, simply do
string niceTime = string.Format("{0:00}:{1:00}", minutes, seconds);
There's a couple of possibilities you have with the formats here: {0:#.00} would give you something like 3.00 or 10.12 or 123.45. For stuff like scores, you might want something like {0:00000} which would give you 00001 or 02523 or 20000 (or 2000000 if that's your score ;-) ). Basically, the formatting part allows any kind of formatting (so you can also use this to format date times and other complex types). Basically, this means {indexOfParameter:formatting}

Struts: Highlighting Date Range in Calendar (JQuery/Javascript)

Thank you very much in advance for reading.
I'm programming a web application in Struts.
I'm using a JQuery datepicker 1.7 as a calendar on the sidebar on the user's view. (It's the best option I could find)
I would like to highlight in the calendar a group of days starting from initial date (startDate) until the end (endDate) of several reminders I have in an array. This way, the user will be able to see on the calendar of the application, all the available days he has left in order to take action for each of his reminders.
I already have my array of reminders which I can access from the view. I was able to implement it properly thanks to guidance from a great fellow around here.
This is my javascript function for the datepicker:
$(function(){
$('#datepicker').datepicker({
flat: true,
numberOfMonths: [1,1],
dateFormat: 'dd/mm/yy',
beforeShowDay: highlightDays
});
There is another function, namely, highlightDays which is the one that is causing me trouble:
It's parameter is the array "reminders" such that reminders[i].start is the startDate, and reminders[i].end is the endDate of reminder i, with i = number of reminders in the array.
function highlightDays(reminders) {
for (var i = 0; i < reminders.length; i++) {
/** Below:
*If startDate is smaller than endDate,
*then highlight the days inbetween.
*/
if (new Date(reminders[i].start).toString() <=
new Date(reminders[i].end).toString() ){
return [true, 'ui-state-highlight'];
}
} //Otherwise do not highlight
return [true, ''];
}
The problem is the calendar won't even show up when I open the application and log in. And the Apache Log or output do not show any errors. I'd like to know, where do you think I might be wrong? I will keep investigating, but I'd really appreciate your input!
P.D: I can access the elements of the array of reminders (endDate, startDate) and print these dates on screen, so that means the reminders array is not empty.
Thank you once again for taking the time to read.
From http://jqueryui.com/demos/datepicker/#event-beforeShowDay
The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name(s) or '' for the default presentation, and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.
Your callback function is not accepting a single date as a parameter, it's taking an array of date ranges.
You might want to figure out how to call the highlightdays function inside the loop of remdinder values.

How do I use Android's Handler.PostDelayed to make an event happen at a specified time?

I want to have my application execute code at a point in the future.
I want to do:
Date now = new Date();
for (Date beep : scheduledBeeps) {
if (beep.after(now))
{
Logger.i("adding beep");
m_beepTimer.postAtTime(beepNow, beep.getTime());
}
}
In the log I can see 4 beeps added, however they never fire. I'm assuming it has something to do with uptimeMillis, but I'm not sure what to do.
You will have to get the difference between now and beep.gettime() and pass it to postattime function. Since uptime is used as base, it may not be accurate if the phone goes to deep sleep.
beep.gettime - now + SystemCLock.uptimeMillis()
should be passed to postattime function
You are currently passing a very large number equivalent to current milliseconds from jan 1 1970.
You could use the Calendar class to set a certain point in time.
Calendar beepTime = Calendar.getInstance();
beepTime.set(Calendar.DAY_OF_MONTH, 2);
beepTIme.set(Calendar.HOUR_OF_DAY, 01);
beepTime.set(Calendar.MINUTE, 55);
beepTime.set(Calendar.SECOND, 00);
getInstance will set it to the current time, and you can change any variable you like, such as the ones above. For example this would create a time at 1:55 on the 2nd of the current month. You would then set this to be the time to go off with
beepTime.getTimeInMillis()
just pop that into your postAtTime method
Edit: Also I don't know enough about your problem to say for sure, but it may be better to use AlarmManager. I know that that still works even if the program is not running, whereas I don't think PostDelayed does. Feel free to correct me if I'm wrong!

Categories

Resources