如何检测日期是在这个还是下个星期的java?
问题描述:
如果我有一个事件的日期,如2011-01-03,如何检测是否在这个或下周的java?任何示例代码?
If I have a date of an event, such as 2011-01-03, how to detect if it is within this or next week in java ? Any sample code ?
编辑:
我以为这是一个简单的问题,我以为本周我吃的是:从过去的太阳到这个星期六,下周是从下个星期到星期六。
I thought it was a simple question, it turned out more complex than I thought, what I meat this week is : from this past Sun to this Sat, next week is from next Sun to the Sat after that.
答
这样做:
Calendar c=Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
c.set(Calendar.HOUR_OF_DAY,0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
DateFormat df=new SimpleDateFormat("EEE yyyy/MM/dd HH:mm:ss");
System.out.println(df.format(c.getTime())); // This past Sunday [ May include today ]
c.add(Calendar.DATE,7);
System.out.println(df.format(c.getTime())); // Next Sunday
c.add(Calendar.DATE,7);
System.out.println(df.format(c.getTime())); // Sunday after next
结果:
Sun 2010/12/26 00:00:00
Sun 2011/01/02 00:00:00
Sun 2011/01/09 00:00:00
前两周之间的任何一天是本周,最后两个之间的任何一个是下周。
Any day between the first two is this week, anything between the last two is next week.