[英]How to check if event is less than 30 minutes?
I am creating an events. 我正在创建一个事件。 I want to check if the event is less than 30 minutes.
我想检查事件是否少于30分钟。 I don't want to allow user to add the event if it is less than 30 minutes.
如果时间少于30分钟,我不想允许用户添加事件。
I am storing the time in date format. 我以日期格式存储时间。
Date format is 日期格式为
df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
I have two dates datefrom
and dateto
. 我有两个日期
datefrom
和dateto
。 I tried to get the minutes of datefrom
and dateto
and minus the from minutes from to minutes and if total minutes are less than 30 then the user can't add the event. 我试图获取
datefrom
和dateto
的分钟数,并减去from分钟从到分钟数,如果总分钟数少于30,则用户无法添加该事件。
But it's not working well. 但是效果不好。 Every time the event can't be added.
每次无法添加事件。
My attempt was : 我的尝试是:
long frmMins = datefrom.getMinutes();
long toMins = dateto.getMinutes();
long calMinutes = toMins - frmMins;
else if (calMinutes < 30) {
showAlert("Event can not be less than 30 Minutes.");
}
Log of event: 事件日志:
Date: Sat Mar 19 16:00:00 GMT+05:30 2016,Todate: Sat Mar 19 23:59:00 GMT+05:30 2016
To convert in millis and then in minutes my attempt was this: 要转换为毫,然后在几分钟内,我的尝试是:
long frmMillis = datefrom.getTime();
long frmMins = TimeUnit.MILLISECONDS.toMinutes(frmMillis);
long toMillis = dateto.getTime();
long toMins = TimeUnit.MILLISECONDS.toMinutes(toMillis);
long calMinutes = toMins - frmMins;
But it gives minus values most of the times. 但大多数情况下,它会提供负值。
How can I do this? 我怎样才能做到这一点?
You can try with this also - 您也可以尝试-
long diff = dateto.getTime() - datefrom.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
if (diffMinutes < 30) {
showAlert("Event can not be less than 30 Minutes.");
}
Since we are doing it in android i would suggest you use alarm manager
to achieve this 由于我们在android中进行操作,因此建议您使用
alarm manager
来实现
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
AlarmManager.INTERVAL_HALF_HOUR,
AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);
This sends an alarm for every half an hour. 这每半小时发送一次警报。 Now you need a reciver to handle the event rest is upto your app logic here is a complete working example http://www.vogella.com/tutorials/AndroidServices/article.html
现在,您需要一个收银机来处理事件休息,具体取决于您的应用程序逻辑,这里是一个完整的工作示例http://www.vogella.com/tutorials/AndroidServices/article.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.