将日历设置为特定日期? [英] Set the Calendar to a specific date?
问题描述
我想设置一个带有特定日期通知的提醒。然后,我现在将AlarmManager与NotificationManager一起使用。当我从dateDialog设置选定的日期时,提醒正在起作用。如何将日历值设置为固定时间的警报?我从这里得到当前日期和时间:
I want to set a reminder with notification on a specific date. Then I am using AlarmManager with NotificationManager currently. When I set selected date from dateDialog, the reminder is working. How can I put calendar value on alarm set with fixed time? I get the current date and time from this :
Calendar calendar = Calendar.getInstance();
然后我可以手动设置日历,如下所示,它可以正常工作:
and then I can set the calendar manually like below and it's working:
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 13);
calendar.set(Calendar.HOUR, 7);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH, 8);
calendar.set(Calendar.YEAR,2015);
long when = calendar.getTimeInMillis();
但是我的问题是如何将日历设置为明天和上午9:00或设置日历精确到当前日期之后的特定月份(或年份)?我的意思是这样的:
But my question is how can I set the calendar to tomorrow and 9:00 AM or set the calendar exactly to a particular month (or year) later from the current date? I mean something like this :
calendar.add(Calendar.DAY_OF_MONTH, 1);
但它不起作用。
推荐答案
Joda-Time
更新: Joda-Time 项目现在位于维护模式,团队建议迁移到 java.time 类。请参见 Oracle教程。
Joda-Time
UPDATE: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.
尝试使用更好的日期时间库,例如Joda-Time。
Try using a better date-time library, such as Joda-Time.
在Joda-Time中,您可以更改日期,同时保留时间。天。或者反之亦然,在保留日期的同时保留一天中的时间。
In Joda-Time you can change the date while keeping the time of day. Or, vice-versa, keep the time of day while keeping the date.
DateTime now = DateTime.now( DateTimeZone.forID( "America/Montreal" ) ) ;
DateTime todayNoon = now.withTime( 12, 0, 0, 0 ) ;
DateTime midMarchSameYearSameTimeAsNow = now.withDate( now.getYear(), DateTimeConstants.MARCH, 15 );
DateTime tomorrowSameTime = now.plusDays( 1 );
这篇关于将日历设置为特定日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!