如何检测日期是在这个还是下个星期的java? [英] How to detect if a date is within this or next week in java?

查看:140
本文介绍了如何检测日期是在这个还是下个星期的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.

这篇关于如何检测日期是在这个还是下个星期的java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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