大家好, 我上有一个闹钟应用程序工作。当用户设置闹钟时,应用程序应该告诉用户闹钟响了多久。麻烦与转换毫秒到小时和分钟
我正在使用simpleDateFormater将毫秒转换为正确的格式。警报在设定的时间内正确地熄灭,但在格式化的时间内,我总是得到额外的11个小时。
例如。如果现在的时间是凌晨1点,我将闹钟设置为凌晨1点10分。我收到消息说警报设置为11小时10分钟,而不是10分钟。警报在上午1点10分正确地熄灭。
我附上相关的代码,让我知道如果你需要看到更多的代码。
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);
这里是我的问题:1。 这里是来自11个小时? 2.我该如何摆脱它?
感谢您花时间阅读本文,并感谢您提供的任何帮助。
我在毫秒工作中遇到了很多麻烦,我总是发现在Android中纳秒更可靠。你有没有尝试过使用System.nanoTime()而不是System.currentTimeMillis()? –