How to calculate the date from iso8601 weeks in Java
How to get dates of a week (I know week number)? For ISO 8601 week number does not use any libraries or calendars in Java?
Solution
Update: the concepts described here still apply, but the code is outdated The joda time project is now maintenance mode. It is recommended to migrate to Java Time course See Java. Java in answer by szulc Time code
Brief answer
DateTime dateTimeStart = new DateTime( "2003-W01-1",DateTimeZone.UTC ); // Joda-Time 2.4. DateTime dateTimeStop = dateTimeStart.plusWeeks( 1 );
For details, please read on
Avoid j.u.date
Old Java bundled with Java util. Date and Java util. The calendar class is a well - known nuisance and should be avoided Sun and its partners have added a lot of simplicity to the Java library, but not all of it is good The date - time class is probably the worst
In addition, these courses have weak support for ISO 8601 weeks See this answer for details
ISO weekly rule
You can write your own code using these classes, but I don't recommend doing so The rules for calculating ISO weeks are simple:
>Week 1 is the first Thursday of the calendar year. > Monday is the first day of the week
Jorda time
In their location, a common alternative is a library called joda time The library includes excellent support for ISO week
Just add one Jar files can be easily added to the project
Other sample code
See this other answer of mine or this one for example code to get a date and time from an ISO week number
java. time
Java 8 has a new date time frame, inspired by joda time, which can be found in Java Found in time package
Add library
Java is built to mix libraries together This is one of the main purposes of object - oriented programming and late binding The comment on your question refers to the very common situation that the boss unreasonably or ignorant prohibits adding libraries Although such prohibitions are justified, they are rare
It is forbidden to add libraries and cans in Java, just as it is forbidden to hook a trailer on a vehicle equipped with a hook
The old date and time course is really bad. Many of us add joda time to most new projects as a habit
Half open
In datetime work, the common method of defining time span is "semi open" method This means that the beginning is inclusive and the end is exclusive Therefore, the standard week starts at the first time of Monday and ends at the first time of the next Monday Search for stackoverflow Com for more discussion and examples
Text representation of ISO week
ISO 8601 defines the method of representative a standard week even within a week
Take the year, a hyphen and a w separator. The number of weeks represents the whole week: yyyy-www. Add hyphens and day of week numbers to determine the day of the week: yyyy-www-d
Joda time understands this format, as shown in the following code example
Sample code
This is some joda time 2.4 code Search for stackoverflow Com for discussion and examples of these concepts This question and this answer almost repeat with many other answers
int year = 2003; int week = 1; // Domain: 1 to 53. // Build a String in ISO 8601 Week format: YYYY-Www-D // Hard-coding a `1` for Monday,the standard first-day-of-week. String input = ( String.format( "%04d",year ) + "-W" + String.format( "%02d",week ) + "-1" ); // Specify the time zone by which to define the beginning of a day. DateTimeZone timeZone = DateTimeZone.UTC; // Or: DateTimeZone.forID( "America/Montreal" ); // Calculate beginning and ending,using Half-Open (inclusive,exclusive) approach. DateTime dateTimeStart = new DateTime( input,timeZone ); DateTime dateTimeStop = dateTimeStart.plusWeeks( 1 ); // Use Joda-Time's tidy Interval class to represent the entire week. Use getters to access start and stop. Interval weekInterval = new Interval( dateTimeStart,dateTimeStop ); // Is today in that week? Joda-Time has handy methods: contains,isBefore,isAfter,overlap. boolean isTodayInThatWeek = weekInterval.contains( DateTime.Now() );
Dump to console
System.out.println( "input: " + input ); System.out.println( "dateTimeStart: " + dateTimeStart ); System.out.println( "dateTimeStop: " + dateTimeStop ); System.out.println( "interval: " + interval ); System.out.println( "isTodayInThatWeek: " + isTodayInThatWeek );
When running
input: 2003-W01-1 dateTimeStart: 2002-12-30T00:00:00.000Z dateTimeStop: 2003-01-06T00:00:00.000Z interval: 2002-12-30T00:00:00.000Z/2003-01-06T00:00:00.000Z isTodayInThatWeek: false