使用Calendar
我可以获得当天的周,年和所有详细信息。如何导航到该周的特定日期?
说,calendar.get(Calendar.DAY_OF_WEEK);
返回3,表示星期二。现在,我想在那个星期的星期五或那个星期的任何其他日子说。我怎么能这样做?
感谢您的回复。我想我需要让场景更清晰。 基本上,我试图在指定的时间段内禁用系统中的电子邮件警报。 我得到的价值如下: disableStart =“FRIDAY-19:00” disableEnd =“SUNDAY-19:00”
现在,我需要验证是否应该在特定时间发送电子邮件。 例如如果今天=星期四任何时候,发送电子邮件 但是,如果今天=星期六,任何时间都不能按照上述值发送。
答案 0 :(得分:25)
如果我理解正确,您可以使用Calendar.set(字段,值)方法。
SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(f.format(c.getTime()));
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(f.format(c.getTime()));
生成输出
6
08-10-2010
3
05-10-2010
答案 1 :(得分:6)
Calendar c = Calendar.getInstance();
Date date = new Date();
c.setTime(date);
System.out.println("Today: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("MONDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
System.out.println("TUESDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
System.out.println("WEDNESDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
System.out.println("THURSDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
System.out.println("FRIDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
System.out.println("SATURDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println("SUNDAY: " + c.getTime());
给出:
Today: Fri Oct 08 15:45:14 CEST 2010
MONDAY: Mon Oct 04 15:45:14 CEST 2010
TUESDAY: Tue Oct 05 15:45:14 CEST 2010
WEDNESDAY: Wed Oct 06 15:45:14 CEST 2010
THURSDAY: Thu Oct 07 15:45:14 CEST 2010
FRIDAY: Fri Oct 08 15:45:14 CEST 2010
SATURDAY: Sat Oct 09 15:45:14 CEST 2010
SUNDAY: Sun Oct 10 15:45:14 CEST 2010
哪种接缝意味着,至少在我的系统上,星期几是星期几。
答案 2 :(得分:2)
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
答案 3 :(得分:2)
感谢Kevin和Maurice的回答。他们真的给了我起点。
我以此测试代码结束,万一它可以帮助任何人。
private static Date getTimeForAnyDayInWeek(int nDay, int nHour, int nMin)
{
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
Date date = Calendar.getInstance().getTime();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, nDay);
c.set(Calendar.HOUR_OF_DAY, nHour);
c.set(Calendar.MINUTE, nMin);
return c.getTime();
}
public static void main(String[] args)
{
Date start = getTimeForAnyDayInWeek(6, 19, 00);
Date end = getTimeForAnyDayInWeek(8, 19, 00);
Date c = new Date();
if (start.before(c) && c.before(end))
System.out.println("BLOCK");
else
System.out.println("SEND");
}
谢谢, Anubhav
答案 4 :(得分:2)
这是jodatime如此优秀的完美示例,这是我的类似代码
DateTime dt = new DateTime(); //current datetime, jodatime format
DateTime fridayLastWeek = dt.minusWeeks(1).dayOfWeek().setCopy("Friday");
Date convertedtorubbishdateformat = fridayLastWeek.toDate();
我过去常常浪费他的标准日期/日历。然后我得到了jodatime,你不会后悔,它显然将来会被用作标准java的一部分。我没有这么做,打扰了罐子多年,我希望我做了,你不会后悔的。