Android 如何将 Java.util.calendar 设置为将来的特定时间段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12649288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:19:46  来源:igfitidea点击:

How to set Java.util.calendar to a specific time period in the future

androidcalendar

提问by NewGradDev

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 specificdate 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:

我正在使用 Java 的日历在特定日期和时间设置闹钟。当用户选择特定日期和时间时,我知道如何进行这项工作。例如,如果用户想在 2013 年 7 月 17 日上午 10:45 设置闹钟,我使用以下代码:

//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);

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?

当我想在特定日期和时间设置闹钟时,上述所有代码都非常有效。我的问题是,如何设置一个日历实例,让用户希望闹钟在当前日期和时间 20 分钟后响起?所以如果当前时间是下午 6:50,我需要闹钟在晚上 7:10 响起。如何以编程方式设置?

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.MINUTEvariable. 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!

我尝试通过 Java.util.calendar 的内置方法获取当前日期和时间,并尝试向Calendar.MINUTE变量添加 20 分钟。但是,如果当前时间距午夜(日期会改变)不到 20 分钟,或者距另一个小时(时间会改变)不到 20 分钟,我认为这不会奏效。我怎样才能解决这个问题?感谢你的帮助!

采纳答案by digidigo

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

您想查看 calendar.add ,如果溢出,它将增加下一个字段。

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

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

回答by Olu Smith

You can also try this

你也可以试试这个

 Calendar c = Calendar.getInstance();
 c.setTime(new Date()); // 
 c.add(Calendar.YEAR, 5); // Add 5 years to current year
 c.add(Calendar.DATE, 5); // Add 5 days to current date
 c.add(Calendar.MONTH, 5); // Add 5 months to current month
 System.out.println(c.getTime());

回答by Chetan

You can try the calendar.set methods specified in this links. How to set the calendar in android for particular hourThis worked for me , when I wanted to run the service on the specified time.

您可以尝试此链接中指定的 calendar.set 方法。 如何在特定时间在 android 中设置日历这对我有用,当我想在指定的时间运行服务时。