0

我想在特定的日期和时间设置闹钟。我正在使用 webservice 获取我的日期和时间。我已经解析并拆分了日期和时间并使用了 SimpleDateFormat,现在我想将此日期和时间放在 [alarmManager.set(AlarmManager.RTC_WAKEUP,dt,pendingIntent);] 但我的闹钟在给定时间不起作用

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

请帮助提前谢谢

4

2 回答 2

0

您应该传递给Alarm.set的时间是一个long并且getDate (已弃用)返回一个 int。

尝试getTime()

于 2011-01-21T13:49:38.950 回答
0

AlarmManager 在 UTC 时区以毫秒为单位使用时间...所以时间解析必须考虑时区

因此,在 SimpleDateFormat 新实例之后,您可以设置 UTC 时区

例子 :

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy"); sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

那么你的解析就可以了,并且与 AlarmManager 服务兼容。

于 2011-08-16T17:10:53.337 回答