简体   繁体   English

设置当前或将来日期的警报

[英]Setting up alarm for current or future date

I am currently grabbing a date format from the database and it has the format of HH:mm eg 10:00. 我目前正在从数据库中获取日期格式,其格式为HH:mm,例如10:00。

Then I change the time format so that it can be in milis to set up a repeating alarm: 然后,我更改时间格式,以便可以设置重复警报的时间:

SimpleDateFormat format = new SimpleDateFormat("HH:mm");
String time = collectionString;

long timeOfFirstCollectionInMillis = format.parse(time).getTime();
System.out.println(collectionInMillis);

//Set Alarm to Repeat
manager.setRepeating(AlarmManager.RTC_WAKEUP,collectionInMillis, interval, pendingIntent);

The problem is it thinks that the date has already passed because there's no date attached to the time (I think). 问题在于它认为日期已经过去,因为时间没有附加日期(我认为)。 How can I set it so that if the time has passed, like it's 11:00am but the time was supposed to be for 10:00am, to set it for a future date and if the the time is 9:00am but the time is for 10:00am for the current date? 我该如何设置它,以便如果时间已过,例如现在是11:00 am,但时间应该是10:00 am,则将其设置为将来的日期,如果时间是9:00 am,但时间是当前日期上午10:00?

I would use the Calendar class for this. 我将为此使用Calendar类。 So basically: 所以基本上:
- set the time to a calendar and also set the date to today -将时间设置为日历,并将日期设置为今天
- compare that with a current timestamp -将其与当前时间戳进行比较
- if it is before the current time then set it to a future date (next hour, next day, ...) -如果在当前时间之前,则将其设置为将来的日期(下一小时,第二天,...)
- if it is after the current time you can use it as is -如果在当前时间之后,您可以按原样使用它

So roughly: 大致来说:

Calendar now = Calendar.getInstance();
now.setTime(new Date());

Calendar cal = Calendar.getInstance();
cal.setTime(timeOfFirstCollectionInMillis);
cal.set(Calendar.YEAR, now.get(Calendar.YEAR));

Do the same for month and day...and then: 对月份和日期执行相同的操作,然后:

if (cal.before(now)) {
   // increase
} else {
  timeOfFirstCollectionInMillis = cal.getTime();
}

I am guessing the output time here might of 24 hour format like for 11:00 pm it might be 23:00. 我猜这里的输出时间可能是24小时格式,例如晚上11:00,可能是23:00。

Hope this helps. 希望这可以帮助。

Joda-Time 乔达时间

Here is the same kind of code as in the other answer by Carsten but using the Joda-Time library. 这与Carsten其他答案中的代码相同,但使用的是Joda-Time库。 That answer basically works but fails to consider the crucial issue of time zone. 该答案基本上有效,但是没有考虑时区的关键问题。 And the java.util.Date/.Calendar classes are notoriously troublesome and should be avoided. 而且java.util.Date/.Calendar类非常麻烦,应该避免。

LocalTime time = LocalTime.parse( "10:00" );
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( zone );
DateTime target = now.withTime( now.getHourOfDay(), now.getMinuteOfHour(), now.getSecondOfMinute(), now.getMillisOfSecond() );
if ( target.isBefore( now ) ) {
    target = target.plusDays( 1 );
}
Duration duration = new Duration( now, target );
long millisUntilTarget = duration.getMillis();

In real work I would add a bit of padding. 在实际工作中,我会添加一些填充。 If millisUntilTarget were a very small number, that small duration might elapse before the rest of the app could sound the alarm. 如果millisUntilTarget是一个非常小的数字,那么可能会在应用程序的其余部分发出警报之前经过很短的持续时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM