Joda Time - Receive All Weeks of the Year

Is there a way to get all the weeks of the year plus the days of the beginning and end of each week? (With Joda-Time )

something like this (2012):

Week: 21 Start: 05/21/2012 End: 05/27/12

thanks for the help

+6
source share
3 answers

Try the following:

SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy"); Period weekPeriod = new Period().withWeeks(1); DateTime startDate = new DateTime(2012, 1, 1, 0, 0, 0, 0 ); DateTime endDate = new DateTime(2013, 1, 1, 0, 0, 0, 0 ); Interval i = new Interval(startDate, weekPeriod ); while(i.getEnd().isBefore( endDate)) { System.out.println( "week : " + i.getStart().getWeekOfWeekyear() + " start: " + df.format( i.getStart().toDate() ) + " ending: " + df.format( i.getEnd().minusMillis(1).toDate())); i = new Interval(i.getStart().plus(weekPeriod), weekPeriod); } 

Please note that week numbers start at 52, and then go from 1 to 51, since January 1 is not Sunday.

If instead you want to see the dates of each Monday-Sunday week:

 SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy"); Period weekPeriod = new Period().withWeeks(1); DateTime startDate = new DateTime(2012, 1, 1, 0, 0, 0, 0 ); while(startDate.getDayOfWeek() != DateTimeConstants.MONDAY) { startDate = startDate.plusDays(1); } DateTime endDate = new DateTime(2013, 1, 1, 0, 0, 0, 0); Interval i = new Interval(startDate, weekPeriod); while(i.getStart().isBefore(endDate)) { System.out.println("week : " + i.getStart().getWeekOfWeekyear() + " start: " + df.format(i.getStart().toDate()) + " ending: " + df.format(i.getEnd().minusMillis(1).toDate())); i = new Interval(i.getStart().plus(weekPeriod), weekPeriod); } 
+5
source

Never Used Joda Time. I would do something like this:

  • Create a class with a week number and two DateTimes (start, end)
  • Create a list of this class
  • Iterate over the year (week to week) and save the current week in the list.

The way I will do this with the standard java api calendar. Joda Time is probably a little lighter, I don't know.

+1
source

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 ) ; // Prepare for next loop. yw = yw.plusWeeks( 1 ) ; } 

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 .

0
source

Source: https://habr.com/ru/post/916455/


All Articles