简体   繁体   中英

Get Date of the first day in the first week of a year in Joda Time

I have a Java date: tempDate = Sun Jan 01 00:00:00 GMT+01:00 2012.

I would like to create a new Date, that is the first day of the first week in the year of tempDate.

That is:

  Mon Jan 02 00:00:00 GMT+01:00 2012

When I try to use Joda Time:

DateTime cal = new DateTime(tempDate).withWeekOfWeekyear(1).
                     withDayOfWeek(DateTimeConstants.MONDAY);

I get what I want, but in the previous year:

Mon Jan 03 00:00:00 GMT+01:00 2011

How can I make this work in Joda Time?

You must understand the difference between a DateTime's year and its week year.

In Joda-Time , in accordance with the ISO 8601 standard , Monday is the first day of the week. Yet years do not always start on a Monday. The first week of a year, still according to the ISO standard, is the week that contains the first Thursday of that year. As a consequence certain days of a year may fall into a week of the previous or the next year.

In your example 01/01/2012 is in fact such a day. It belongs to week 52 of 2011. So its year (2012) is different from its week year (2011). Consequently, if you change the date to the first week of its week year, you'll get a DateTime in the first week of 2011.

To make this work consistently you simply have to make sure your week year is the same as your year :

DateTime onTheFirstDayOfTheFirstWeek = dateTime.withWeekyear(dateTime.getYear()).withWeekOfWeekyear(1).withDayOfWeek(1);

BTW the workaround in your comment will not work consistently, specifically it will fail when the system time falls in a day that has a year different from it's week year.

The answer by bowmore is correct, answering the question.

If you did indeed want to follow the ISO 8601 standard definition of a week-of-year and first week , here is some example code using Joda-Time 2.3.

Note the call to withTimeAtStartOfDay to get the first moment of the day. Days do not always begin at 00:00:00 time.

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );

DateTime now = new DateTime( timeZone );
DateTime firstWeekStart = now.withWeekOfWeekyear(1).withDayOfWeek( DateTimeConstants.MONDAY ).withTimeAtStartOfDay();
DateTime firstWeekStop = firstWeekStart.plusWeeks( 1 );
Interval firstWeek = new Interval( firstWeekStart, firstWeekStop );

Dump to console…

System.out.println( "now: " + now );
System.out.println( "firstWeekStart: " + firstWeekStart );
System.out.println( "firstWeekStop: " + firstWeekStop );
System.out.println( "firstWeek: " + firstWeek );
firstWeek: 2013-12-30T00:00:00.000+01:00/2014-01-06T00:00:00.000+01:00

When run…

now: 2014-02-07T12:49:33.623+01:00
firstWeekStart: 2013-12-30T00:00:00.000+01:00
firstWeekStop: 2014-01-06T00:00:00.000+01:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM