简体   繁体   中英

Trouble with converting milliseconds to hours and minutes

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.

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM