0

使用此代码,我可以获取一些字符串,例如“Wed 10:10”,

String nextAlarm = Settings.System.getString(getContentResolver(),
    Settings.System.NEXT_ALARM_FORMATTED);

但我希望下一个闹钟的值是秒。那可能吗 ?如果我能够读取描述字段的值以及启用此警报的日期,那对我来说也会很好。

4

1 回答 1

1

您需要按照以下方式进行

private void updateAlarmDate(String nextAlarm) {
    if (nextAlarm.length()==0 || nextAlarm == "") {
        return;
    }
    else
    {
        DateFormat sdf = new SimpleDateFormat("EEE hh:mm aa");
        Date alarmDate = null;
        try {
            alarmDate = sdf.parse(nextAlarm);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int alarmDay = alarmDate.getDay();
        Calendar now = Calendar.getInstance();
        Date currentDate = now.getTime();
        int todayDay = currentDate.getDay();
        int daysDiff = 0;
        if (todayDay > alarmDay) {
            daysDiff = ((7 + (alarmDay - todayDay)) % 7) + 1;
        } else {
            daysDiff = ((7 + (alarmDay - todayDay)) % 7);
        }
        now.add(Calendar.DATE, daysDiff);
        AlarmFulldate = now.getTime();
        AlarmFulldate.setHours(alarmDate.getHours());
        AlarmFulldate.setMinutes(alarmDate.getMinutes());
        AlarmFulldate.setSeconds(0);
    }
}    

long millis = AlarmFulldate.getTime() - new Date().getTime(); 变量毫秒将有毫秒。

使用以下代码从毫秒获取天数、小时数和分钟数

days = (int) (millis / (1000 * 60 * 60 * 24));
hours = (int) ((millis - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));
min = (int) (millis - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours))/(1000 * 60);

希望对你有帮助

于 2012-06-12T06:31:33.807 回答