I have a string that may contain value 07:00 or 11:00 0r 15:00
I want to set alarm for that time for today. it doesn't seem to work
One option is to create a calendar object set to midnight, and then offset it by the hours and minutes you have:
String alarm = "13:00";
Calendar c = new GregorianCalendar();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long millis = c.getTime().getTime();
millis += 1000*60*60*Integer.parseInt(alarm.substring(0, 2));
millis += 1000*60*Integer.parseInt(alarm.substring(3, 5));
Demo
An alternative to this would be to use two SimpleDateFormat masks to parse out the timestamp you want.
Related
I am trying to set weekly monday reminder in calendar at 9 a.m.
Following is the code
final int[] preTimings = {9, 12, 18};
Calendar calendar = Calendar.getInstance(Locale.getDefault());
ContentValues newEvent = new ContentValues();
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
for(int i = 0; i < preTimings.length; i++{
calendar.set(Calendar.HOUR_OF_DAY, preTimings[i]);
long timeInMillis = calendar.getTimeInMillis();
newEvent.put(CalendarContract.Events.CALENDAR_ID, 1);
newEvent.put(CalendarContract.Events.TITLE, preTitle[i]);
newEvent.put(CalendarContract.Events.DTSTART, timeInMillis);
newEvent.put(CalendarContract.Events.DTEND, timeInMillis + 60000);
newEvent.put(CalendarContract.Events.RRULE, "FREQ=WEEKLY;BYDAY=MO");
newEvent.put(CalendarContract.Events.HAS_ALARM, true);
newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:30");
So this program is setting the 12 p.m and 6 p.m correctly on Mondays every week but for some reason it is setting the 9 a.m on Tuesdays every week.
I don't know what is wrong over here. I even tried converting the timeInMillis in an online calculator to know what is the result, but they are correct.
So the issue was on the last line, even though i have specifically declared the timezone, it was setting it incorrectly.
So instead of
newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:30");
It should be
newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, String.valueOf(TimeZone.getTimeZone("UTC")));
Still no idea why it worked for times 11 a.m and above. Kinda weird
so I have this code in Android
DateFormat formatter = new SimpleDateFormat("HH:mm", Locale.getDefault());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millisUntilFinished);
textView.setText(formatter.format(calendar.getTime()));
here is how I am passing the values to this method, where the hours and minutes strings are "8" and "30" for example
Calendar c = Calendar.getInstance();
String s = ApplicationPreferences.getWakeUp(ActivityStage1.this);
String[] separated = s.split("\\:");
String hours = separated[0];
String minutes = separated[1];
c.add(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hours));
c.set(Calendar.MINUTE, Integer.parseInt(minutes));
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long timeUntilStageTwo = (c.getTimeInMillis() - System.currentTimeMillis());
startStageTwoTimer(timeUntilStageTwo);
So the value in the long millisUntilFinished is something like 58441666, which is around 16 hours and 20 min, but for some reason the time it shows at the end in the text view is with 3 hours more, I even tried with different locales passed to SimpleDateFormat, and still the same, why is that happening?
Your Calendar c is in your local UTC+3 timezone while the system clock runs at UTC. Hence the difference of 3 hours.
You can use setTimeZone() to set an explicit timezone on your calendar before computing its millisecond value.
If you prefer to do the computations yourself, you can get the user's timezone with TimeZone.getDefault() and then use getOffset(time) to get the millisecond offset at specified UTC time.
I hope someone can help. Im trying to set up a timer that times from the start of a game and displays this time. The problem is that the following section of code gives me the wrong time. Its in the wrong format, and is out by an hour.
private long startTime;
private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SS");
//Constructor:
startTime = System.currentTimeMillis();
public String getTime() {
long gameTime = System.currentTimeMillis() - startTime;
final Date date = new Date(gameTime);
return timeFormat.format(date);
}
It consistently gives me the output of 01:00:03:203. The seconds are correct, but the 1 hour shouldn't be there, and for format is 3 decimal places instead of the two I thought I specified.
Thank you very much!
Your date is epoch + gameTime. I think you're experiencing a daylight saving shift since the current DST in your location today doesn't match the DST at epoch.
Use a Calendar instead of a Date. Start with today and explicitly wipe out the hour, minute, etc. parts:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 3600000 + 60000 + 1000 + 1);
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SS");
System.out.println(timeFormat.format(cal.getTime()));
The output for the above is: 01:01:01.01
http://ideone.com/DyQcl
Substitute the numbers I have above with gameTime and you're done.
Of course, this may not work once your millisecond ticks exceed the day boundary.
Hi guys this is what i have so far:
PendingIntent sender = PendingIntent.getBroadcast(mainactivity, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
showmsg();
the alarm comes after 10 seconds How can I make it in desire time i want, I have set a timepicker so how can i do it depending on the timepickers time to ring the alarm?
Thanx.
With your TimePicker, call picker.getCurrentHour() and picker.getCurrentMinute(). Use these values to calculate the total milliseconds of the given time of day (12:00 pm would equal 43200000 ms, for example) by multiplying by the milliseconds in an hour (3600000) or minute (60000). Then get the milliseconds of today's date when it started at 0:00. That would all look like this:
//time of day in ms
long totalTimePickerMs = (picker.getCurrentHour() * 3600000) + (picker.getCurrentMinute() * 60000);
//today's date in ms
Calendar c = Calendar.getInstance();
Date d = c.getTime();
long today = d.getDay() + d.getMonth() + d.getYear();
long total = today + totalTimePickerMs;
Essentially you are getting today at midnight (0:00) in milliseconds and adding to it the milliseconds of the specific time of day.
Then like you did before, except set the alarm with total as the second parameter.
I would like to get the current date with the time zeroed out in milliseconds.
Example, if it's 12:69pm today, I want to get the time in milliseconds for today's date with no time...meaning, the time just after midnight (one millisecond or 0 if that works).
I was using the Calendar object but can't seem to figure out how to zero out the time portion.
Here is how to zero the time of a calendar:
Calendar today = Calendar.getInstance();
today.set(Calendar.MILLISECOND, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.HOUR_OF_DAY, 0);
And without calendar:
long d = new Date().getTime();
int offset = TimeZone.getDefault().getOffset(d);
d = ((d + offset)/ 86400000l) * 86400000l - offset;