Reputation: 1932
Hi everyone , I am working on an app that has an alarm clock. When the user sets the alarm , the app should tell the user how long till the alarm goes off.
I am using simpleDateFormater to get the the milliseconds into the right format. The alarm goes off correctly at the set time , but in the formatted time i am always getting an extra 11 hours.
FOr example. if the time now is 1:00 am and I am setting the alarm for 1:10 am . I get the message saying the alarm is set for 11 hrs and 10 mins instead of just 10 minutes.The alarm goes off correctly at 1:10 am.
I am attaching the relevant code , let me know if you need to see any more of the code.
long current = System.currentTimeMillis();
long time_left = set_time-current; // set_time is the time the alarm should go off , and it is set else where in the activity.
String datePattern = "h ' hours and' mm ' minutes'";
SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
String timeLeft_string = formatter.format(time_left);
Toast.makeText(getBaseContext(), "Alarm set for"+timeLeft_string, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("id", mRowId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), mRowId_int, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, set_time, pendingIntent);
Here are my questions : 1. where is the 11 hours coming from ? 2. how do I get rid of it ?
Thanks for taking the time to read this and for any help that you can give.
Upvotes: 0
Views: 186
Reputation: 2364
Just to amplify on Jon Skeet's answer, set_time - current
isn't really a date, at least not semantically. So even if you can get SimpleDateFormat
to work, it's misleading.
Joda Time is a good choice, but if you're not really going to do much more time manipulation than what you described above, I'd just do a little dividing and modding:
final long MILLIS_PER_SECOND = 1000;
final long SECONDS_PER_HOUR = 3600;
final long SECONDS_PER_MINUTE = 60;
long deltaSeconds = time_left / MILLIS_PER_SECOND;
long deltaHours = deltaSeconds / SECONDS_PER_HOUR;
long leftoverSeconds = deltaSeconds % SECONDS_PER_HOUR;
long deltaMinutes = leftoverSeconds / SECONDS_PER_MINUTE;
Toast.makeText(getBaseContext(),
String.format(Locale.US, "Alarm set for %d hours and %d minutes", deltaHours, deltaMinutes),
Toast.LENGTH_SHORT).show();
Upvotes: 1
Reputation: 1504052
I suspect the problem is that you're formatting with the local time zone. You're basically creating a date on the 1st January 1970 UTC, then formatting the time-of-day using your default time zone.
Personally I wouldn't use SimpleDateFormat
at all here - I'd probably use Joda Time, and format this as a Duration
or a Period
, as that's what you're really trying to represent.
If you do want to use SimpleDateFormat
, you probably want to use H
(24-hour format) instead of h
, and set the time zone to UTC.
Upvotes: 4