如何获得过去的星期天和即将到来的星期天在Java? [英] How to get the past Sunday and the coming Sunday in Java?

查看:104
本文介绍了如何获得过去的星期天和即将到来的星期天在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问如何检测Java中的日期是在这个还是下一个星期?但答案令人困惑,所以现在我想如果我能找到过去的星期天和即将到来的星期天,这个星期之间的任何一天,而在下周的星期天和星期天之间的任何一天,我是正确的吗?

I asked How to detect if a date is within this or next week in Java? but the answers were confusing, so now I think if I can find the past Sunday and the coming Sunday, any day in between is this week, and any day between the coming Sunday and the Sunday after that is next week, am I correct ?

所以我的新问题是:如何让过去的星期天和即将到来星期天在Java?

So my new question is : How to get the past Sunday and the coming Sunday in Java ?

推荐答案

如何做:

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天全站免登陆