我正在使用API来获取我的Android应用的电影发行版,发行日期以UTC毫秒为单位。当我获取那个发布日期并将其设置在用户的手机上时,它将在他的时区进行转换并被调用。
示例:
电影的发行日期(以毫秒为单位):
1509667200000
到UTC时间和日期:
2017年11月3日星期五00:00:00
到本地时间和日期:(东部时间,我的时区)
2017年11月2日星期四20:00:00
设置闹钟时
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(releaseDate);
Date date = calendar.getTime();
alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent);
警报被称为2017年11月2日20:00:00,但它需要在11月03日午夜被呼叫。
谢谢
答案 0 :(得分:2)
我做了什么,现在可以正常使用了:
// Calendar
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.setTimeInMillis(releaseDate);
Date date = calendar.getTime();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}
答案 1 :(得分:1)
您可以尝试将时区设置为UTC
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
答案 2 :(得分:0)
我已经使用o闹钟应用了几天。 我已经为学校课程表设置了警报模块。 我所做的是...
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet);
}};
private void setAlarm(Calendar targetCal){
textAlarmPrompt.setText(
"\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n");
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
// alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
}