如何为java.util.Calendar设置为在未来的特定时间段 [英] How to set Java.util.calendar to a specific time period in the future

查看:655
本文介绍了如何为java.util.Calendar设置为在未来的特定时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java的日历设置在特定的日期和时间报警。我知道如何当用户选择一个的具体的日期和时间,使这项工作。例如,如果用户想要设置2013年7月17日的警报在上午10:45,我用下面的code:

I'm using Java's calendar to set an alarm at a specific date and time. I know how to make this work when the user selects a specific date and time. For example, if the user wants to set an alarm on July 17th, 2013 at 10:45AM, I'm using the following code:

//Get the calendar instance.
Calendar calendar = Calendar.getInstance();       

//Set the time for the notification to occur.
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.DAY_OF_MONTH, 17);                 
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);

以上所有code的作品真的很好,当我想在特定的日期和时间可设置闹钟。我的问题是,我该怎么设置,用户用户想要报警,从当前日期和时间熄灭20分钟,日历实例?因此,如果当前时间是6:50 PM,我需要在报警7:10 PM熄灭。我怎样才能设置此编程?

All of the above code works really well when I want to set an alarm at a specific date and time. My question is, how can I set a calendar instance where the user user wants an alarm to go off 20 minutes from the current date and time? So if the current time is 6:50PM, I need the alarm to go off at 7:10PM. How can I set this programmatically?

我就先通过java.util.Calendar中内置的方法当前日期和时间,并试图将20分钟的 Calendar.MINUTE 变量。不过,我不认为这会做的伎俩,如果当前时间小于从午夜20分钟路程(日期会改变),或20分钟从一个小时(小时会发生变化)的距离。我怎样才能解决这个问题呢?感谢您的帮助!

I tried to get the current date and time via the Java.util.calendar's built-in methods and tried adding 20 minutes to the Calendar.MINUTE variable. However, I don't think this will do the trick if the current time is less than 20mins away from midnight (the date will change), or 20mins away from another hour (the hour will change). How can I get around this problem? Thanks for all your help!

推荐答案

您想看看calendar.add,这将增加下一个字段,如果你得到溢出。

You want to look at calendar.add , it will increment the next field if you get overflow.

http://developer.android.com/reference/java/util/ Calendar.html

这篇关于如何为java.util.Calendar设置为在未来的特定时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆