Joda-Time: Get max number of weeks for particular

2019-03-27 02:22发布

问题:

How do I get the maximum number of weeks for a particular year with Joda-Time?

回答1:

new DateTime().withYear(PARTICULAR_YEAR).weekOfWeekyear().getMaximumValue();

@cody Comment

Difference between the two methods .withWeekOfWeekyear() and .withWeekyear()

Example

new DateTime().withDate(2011, 1, 2);   

2/1/2011       : Sunday, last day of last week of 2010 (27/12/2010 - 2/1/2011)   
dayOfWeek      : 7
weekOfWeekyear : 52  
weekyear       : 2010


new DateTime().withDate(2011, 1, 3);

3/1/2011       : Monday, first day of first week of 2011 (3/1/2011 - 9/1/2011)
dayOfWeek      : 1
weekOfWeekyear : 1
weekyear       : 2011


回答2:

I know that replying to a question this old is a necromancy but the accepted answer (by lschin) has a bug - while most of the time it will return correct value, it will sometimes return a wrong one, depending on when the calculation was performed. For example:

new DateTime().withYear(2014).weekOfWeekyear().getMaximumValue();

If you run the above on 28 Dec 2014, you'll get the correct value of 52. However, the same code when run just a day later, on 29 Dec 2014, will return the value of 53. Which is obviously a problem because a given year has a constant number of weeks (not to mention the fact that you wouldn't want your methods work only some of the time). You can test this by changing your system date or by using a fixed date instead of new Date().

The problem here is weekOfWeekyear() which can increment the given year value if new DateTime() happens to return a near end-of-year date. This is not a problem when the current and the next year both have the same amount of weeks but will be a problem otherwise. So, to correct this, use:

new DateTime().withWeekYear(2014).weekOfWeekyear().getMaximumValue();
                   ^^^^

This will make sure you won't change increment a year on those rare occasions and get consistently correct results.



回答3:

tl;dr

org.threeten.extra.YearWeek.of( 2018 , 1 ).is53WeekYear()

…or…

org.threeten.extra.YearWeek.of( 2018 , 1 ).lengthOfYear()

java.time

The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

Week?

What is your definition of week? Does week # 1 contain January 1st? Or does week # 1 contain a particular day-of-week such as Sunday? Is week # 1 the first week to contain only days of the new year? Some other definition?

ISO 8601

If your definition agrees with the ISO 8601 standard, that would mean week # 1 contains the first Thursday of the calendar year and Monday starts each week. So there are either 52 or 53 weeks in the year. And the last few or first few days of the calendar may land in the previous/next week-based year.

ThreeTen-Extra YearWeek class

For standard weeks, add the ThreeTen-Extra library to your project. These classes further the functionality of the java.time classes built into Java 8 and later.

Get a week from your desired week-based year number.

YearWeek yw = YearWeek.of( 2018 , 1 ) ;

Perhaps you want the current year-week.

LocalDate ld = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
YearWeek yw = YearWeek.from( ld ) ;

Get the number of weeks in that year, either 52 or 53.

int weeksCount = yw.lengthOfYear() ;

Or simply ask if that year has 53 weeks.

boolean has53 = yw.is53WeekYear() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.