如何在爪哇过去的星期天和即将到来的星期天?

时间:2010-12-27 01:52:17

标签: java date

我问How to detect if a date is within this or next week in Java?但答案令人困惑,所以现在我想如果我能找到过去的星期日和即将到来的星期日,那么这一周的任何一天都是本周,以及即将到来的星期日和星期日之间的任何一天在下周之后,我是否正确?

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

9 个答案:

答案 0 :(得分:19)

我以为我为后代添加了Java 8解决方案。

final LocalDate today = LocalDate.of(2015, 11, 20);
final LocalDate nextSunday = today.with(next(SUNDAY));
final LocalDate thisPastSunday = today.with(previous(SUNDAY));

此方法也适用于其他时间类,如ZonedDateTime。如上所述,它假定以下静态导入:

import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.next;
import static java.time.temporal.TemporalAdjusters.previous;

答案 1 :(得分:13)

这个怎么样:

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

前两天之间的任何一天都是本周,最后两周之间的任何一天都是下周。

答案 2 :(得分:4)

不使用更好的时间/日期套餐......

DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
Calendar now = new GregorianCalendar();
Calendar start = new GregorianCalendar(now.get(Calendar.YEAR), 
        now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH) );

while (start.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
    start.add(Calendar.DAY_OF_WEEK, -1);
}

Calendar end = (Calendar) start.clone();
end.add(Calendar.DAY_OF_MONTH, 7);

System.out.println(df.format(now.getTime()) );
System.out.println(df.format(start.getTime()) );
System.out.println(df.format(end.getTime()) );

如果今天是星期天,它被认为是时间段的开始。如果你想要一周和下周(从你的问题中听起来),你可以在end.add(...)行中替换14而不是7。时间设置为午夜,用于比较开始和结束之间的另一个对象。

答案 3 :(得分:4)

首先,不要使用Java中的Date / Time包。有一个更好的实用程序包叫做Joda-Time - 下载并使用它。

要确定您的时间是在本周,上周还是任何一周,请执行以下操作:

  1. 创建两个Interval对象 - 一个用于上周,一个用于本周
  2. 使用contains(long)方法确定哪个时间间隔包含您要查找的日期。
  3. 有几种很酷的方法可以创造两个背靠背周。您可以设置一周的持续时间,查找第一周的开始时间,并根据该开始时间创建两个间隔。随意找到适合您的其他方式 - 该软件包有多种方法可以达到您想要的效果。


    编辑:

    Joda-Time可以是downloaded here,这是Joda如何做到这一点的一个例子:

    // Get the date today, and then select midnight of the first day of the week
    // Joda uses ISO weeks, so all weeks start on Monday.
    // If you want to change the time zone, pass a DateTimeZone to the method toDateTimeAtStartOfDay()
    DateTime midnightToday = new LocalDate().toDateTimeAtStartOfDay();
    DateTime midnightMonday = midnightToday.withDayOfWeek( DateTimeConstants.MONDAY );
    
    // If your week starts on Sunday, you need to subtract one.  Adjust accordingly.
    DateTime midnightSunday = midnightMonday.plusDays( -1 );
    
    DateTime midnightNextSunday = midnightSunday.plusDays( 7 );
    DateTime midnightSundayAfter = midnightNextSunday.plusDays( 7 );
    
    Interval thisWeek = new Interval( midnightSunday, midnightNextSunday );
    Interval nextWeek = new Interval( midnightNextSunday, midnightSundayAfter );
    
    if ( thisWeek.contains( someDate.getTime() )) System.out.println("This week");
    if ( nextWeek.contains( someDate.getTime() )) System.out.println("Next week");
    

答案 4 :(得分:1)

我最近开发了Lamma Date,旨在解决此用例:

Date today = new Date(2014, 7, 1);      // assume today is 2014-07-01
Date previousSunday = today.previousOrSame(DayOfWeek.SUNDAY);   // 2014-06-29
Date nextSunday = today.next(DayOfWeek.SUNDAY);                 // 2014-07-06

答案 5 :(得分:0)

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html

我推荐Calendar.get(Calendar.DAY_OF_WEEK)

答案 6 :(得分:0)

您可以尝试使用Calendar.WEEK_OF_YEAR字段,该字段为您提供当年内本周的数字表示。

@Test
public void testThisAndNextWeek() throws Exception {
    GregorianCalendar lastWeekCal = new GregorianCalendar(2010,
            Calendar.DECEMBER, 26);
    int lastWeek = lastWeekCal.get(Calendar.WEEK_OF_YEAR);

    GregorianCalendar nextWeekCal = new GregorianCalendar(2011,
            Calendar.JANUARY, 4);
    int nextWeek = nextWeekCal.get(Calendar.WEEK_OF_YEAR);

    GregorianCalendar todayCal = new GregorianCalendar(2010,
            Calendar.DECEMBER, 27);
    int currentWeek = todayCal.get(Calendar.WEEK_OF_YEAR);

    assertTrue(lastWeekCal.before(todayCal));
    assertTrue(nextWeekCal.after(todayCal));
    assertEquals(51, lastWeek);
    assertEquals(52, currentWeek);
    // New Year.. so it's 1
    assertEquals(1, nextWeek);
}

答案 7 :(得分:0)

我的解决方案:

    LocalDate date = ...;
    LocalDate newWeekDate = date.plusDays(1);
    while (newWeekDate.getDayOfWeek() != DayOfWeek.SATURDAY &&
           newWeekDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
        newWeekDate = date.plusDays(1);
    }

答案 8 :(得分:0)

  

给出下个星期日的波纹管代码,您可以轻松找出如何找到过去的星期日。

private static void nextSunday() throws ParseException 
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(new Date());
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        int days = Calendar.SUNDAY - weekday;
        if (days < 0)
        {
            days += 7;
        }
        calendar.add(Calendar.DAY_OF_YEAR, days);

        System.out.println(sdf.format(calendar.getTime()));
    }