报警管理器时间设置问题
我想在特定的日期和时间设置闹钟。我正在使用 webservice 获取日期和时间。我已经解析并分割了日期和时间并使用了 SimpleDateFormat,现在我想将此日期和时间放入 [alarmManager.set(AlarmManager.RTC_WAKEUP,dt,endingIntent );] 但我的闹钟在给定时间不起作用
String str_date= hr+":"+min+":"+sec+" "+dat+"/"+mon+"/"+year;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
ParsePosition position = new ParsePosition(0);
java.util.Date stringToDate = sdf.parse(str_date, position);
dt = stringToDate.getDate();
请帮助 提前致谢
I want to set an alarm at a particular date and time . iam getting my date and time with the webservice.i have parsed and splitted the date and time and used SimpleDateFormat and now i want to put this date and time in [alarmManager.set(AlarmManager.RTC_WAKEUP,dt, pendingIntent );] but my alarm doesnot work on the given time
String str_date= hr+":"+min+":"+sec+" "+dat+"/"+mon+"/"+year;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
ParsePosition position = new ParsePosition(0);
java.util.Date stringToDate = sdf.parse(str_date, position);
dt = stringToDate.getDate();
Please help
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该传递给 Alarm.set 是一个
long
和getDate
(即 已弃用)返回一个 int。尝试使用
getTime()
The time you should pass to Alarm.set is a
long
andgetDate
(which is deprecated) returns an int.Try with
getTime()
AlarmManager 在 UTC 时区使用以毫秒为单位的时间...
因此时间解析必须考虑时区
因此在 SimpleDateFormat new 实例之后您可以设置 UTC 时区
示例:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
那么你的解析就可以了并且与 AlarmManager 服务兼容。
AlarmManager use time in millesecond unit at UTC time zone...
So time parsing have to take the timezone into account
So after SimpleDateFormat new instance you may set the UTC timezone
example :
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
then your parsing will be ok and compatible for the AlarmManager service.