简体   繁体   English

如何检查事件是否少于30分钟?

[英]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 . 我有两个日期datefromdateto 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. 我试图获取datefromdateto的分钟数,并减去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.

相关问题 Unix Cron 验证:不少于 30 分钟 - Unix Cron validation: Not less than every 30 minutes 如果时间跨度重叠且少于30分钟,则对日志记录条目进行分组? - Group logging entries if time span overlap and less than 30 minutes? 如果节点创建的时间戳少于或超过五分钟,我应该如何使服务器检查? - How should I make the server check, if the time stamp made by the node is less than or more than five minutes old? 不到5分钟,Android服务就会停止 - Android service stops after less than 5 minutes 如何检查2个日期之间的差异是否超过20分钟 - How to check if the difference between 2 dates is more than 20 minutes 如何在if / else语句中检查整数且小于或等于100 - How to check for integer and less than or equal to 100 in an if/else statement 如何检查Java的已编译查询中的小于条件? - How to check the less than condition in compiled queries of java? Java 中的 CPLEX,如何检查 model 内部的 IloNumVar 是否小于零? - CPLEX in Java, how to check a IloNumVar is less than zero inside of model? 春天如何每30分钟更新一次缓存? - How to update cache every 30 minutes in spring? 如何运行java函数只需30分钟 - How to run java function for only 30 minutes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM