Get LocalDateTime based on year and week of the year

I have two integers: year and week . I would like to build LocalDateTime from these two values, where the date represents the first day of a particular week in a particular year.

I have already tried this:

 LocalDateTime ldt = LocalDateTime.of(year, 1, 1, 0, 0).plusWeeks(week-1); 

This does not give the correct result, because the first day of the year is not always the beginning of the first week of this year. I can’t figure out how to get the first day of the first week.

Is there an easy way to do this using the Date and Time APIs in Java 8?

Note. I know that I could also create a workaround with answers here , but that seems far-fetched. Maybe this is not so, if so, let me know!

+7
java datetime java-8 java-time
source share
4 answers

The following code will receive LocalDateTime and set it on the first day of this week of the year for the given year:

 int week = 1; int year = 2016; WeekFields weekFields = WeekFields.of(Locale.getDefault()); LocalDateTime ldt = LocalDateTime.now() .withYear(year) .with(weekFields.weekOfYear(), week) .with(weekFields.dayOfWeek(), 1); System.out.println(ldt); 

Please note that the concept of the week does not correspond to the language. Here I used the default locale, but you may need to use the locales provided by the user.

If you want to get rid of part of the time, you can add a call to truncatedTo(ChronoUnit.DAYS) .

+9
source share

You can try to parse the string representation of the day you are interested in:

 private static final DateTimeFormatter YEAR_WEEK = DateTimeFormatter.ofPattern("YYYY-we", Locale.getDefault()); public static LocalDate fromYearWeek(int year, int week) { return LocalDate.parse(year + "-" + week + "-1", YEAR_WEEK); } 

If you need LocalDateTime, you can then use localDate.atTime(...) or localDate.atStartOfDay() .

+3
source share
 LocalDate ld = LocalDate.parse("2012-W48-1", DateTimeFormatter.ISO_WEEK_DATE); 
+2
source share

ThreeTen-Extra

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as YearWeek .

If in β€œweek” you were referring to the standard definition of ISO 8601 of the week of the week, where week No. 1 contains the first Thursday of the year, let the YearWeek class do your work.

 int weekBasedYear = 2015; // Not the calendar year! int weekOfYear = 1; YearWeek yearWeek = YearWeek.of ( weekBasedYear , weekOfYear ); 

You can only get the value for the date represented in the LocalDate object. Indicate which day of the week you want to use the DayOfWeek enum, where Monday is the first day of the week and Sunday is the last day. Remember that the ISO week always starts on Monday and always ends on Sunday.

 LocalDate mondayOfYearWeek = yearWeek.atDay ( DayOfWeek.MONDAY ); LocalDate sundayOfYearWeek = yearWeek.atDay ( DayOfWeek.SUNDAY ); 

Dump for the console. Pay attention to how the week starts in the previous calendar year (2014) and ends in 2015, since January 1, 2015 is Thursday, so it marks the week of ISO No. 1. Next Sunday, January 4, 2015, the week ends ISO No. 1.

 System.out.println ( "yearWeek: " + yearWeek + " starts on mondayOfYearWeek: " + mondayOfYearWeek + " and ends on sundayOfYearWeek: " + sundayOfYearWeek ); 

yearWeek: 2015-W01 starts at mondayOfYearWeek: 2014-12-29 and ends at sundayOfYearWeek: 2015-01-04

+1
source share

All Articles