Joda-Time is in maintenance mode
FYI, the Joda-Time project is now in maintenance mode , and the team advises switching to the java.time classes. See the tutorial from Oracle .
Determine week
You can define a week differently.
I assume you mean the standard ISO 8601 week . Week No. 1 has the first Thursday of the year, starts on Monday, and the weekly year is 52 or 53 weeks. A few days at the end or beginning of a calendar year may land in another weekly year.
java.time
The modern approach uses the java.time classes, and their extension is found in ThreeTen-Extra .
From ThreeTen-Extra, use the YearWeek
class.
YearWeek start = YearWeek.of( 2017 , 1 ) ; // First week of the week-based year 2017.
Get the number of weeks this year, 52 or 53.
int weeks = start.lengthOfYear() ;
... or...
int weeks = ( start.is53WeekYear() ) ? 53 : 52 ;
Loop for each week of the year. For each YearWeek
ask him to create a LocalDate
to start and end this week.
List<String> results = new ArrayList<>( weeks ) ; YearWeek yw = start ; for( int i = 1 , i <] weeks , i ++ ) { String message = "Week: " + yw + " | start: " + yw.atDay( DayOfWeek.MONDAY ) + " | stop: " + yw.atDay( DayOfWeek.SUNDAY ) ; results.add( message ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date
, Calendar
and SimpleDateFormat
.
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval
, YearWeek
, YearQuarter
and more .