java 判断是否同一周,如何检测日期是在这个还是下个星期的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 ?

Edit :

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.

解决方案

How about this :

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

The result :

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值