tl;dr
int iso8601WeekNumber =
ZonedDateTime.now( ZoneId.of ( "America/Montreal" ) )
.get( IsoFields.WEEK_OF_WEEK_BASED_YEAR )
Many definitions of a week
There are many definitions of a week.
For some people the first day of a week is a Sunday, others Mondays, and still others some other day.
For some people the first week of the year contains January 1st, while some other folks count the first week as the one containing that start-of-week day mentioned above, while others count week one as containing a specific day-of-week.
So you should never assume the meaning of a "week" or "week number" without studying the documentation.
Avoid legacy date-time classes
In general, you should avoid the old legacy date-time classes bundled with the earliest versions of Java as they are poorly-designed, confusing, and troublesome.
Here specifically, the java.util.Calendar
class has a definition of week that varies by locale. So do not use this if you want reliable constant results.
ISO 8601
The ISO 8601 standard defines formats and related issues for date-time values. It specifically defines the meaning of a week and week-of-year:
- Week runs from Monday-Sunday, numbered 1-7 where Monday = 1.
- Week # 1 contains the first Thursday of the year, yielding 52 or 53 weeks per year.
Joda-Time
The Joda-Time library uses the ISO 8601 standard definition of week and week-of-year.
However, be aware that the Joda-Time project is now in maintenance mode, with the team advising migration to java.time.
java.time
The java.time classes use the ISO 8601 definition of a week: Week # 1 has first Thursday, with week running Monday-Sunday.
The IsoFields
class defines a week-based year. We can ask for the:
First we get the current date-time.
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now ( zoneId );
Interrogate that date-time object, asking about the standard week-based year.
int week = now.get ( IsoFields.WEEK_OF_WEEK_BASED_YEAR );
int weekYear = now.get ( IsoFields.WEEK_BASED_YEAR );
Dump to console.
System.out.println ( "now: " + now + " is week: " + week + " of weekYear: " + weekYear );
now: 2016-01-17T20:55:27.263-05:00[America/Montreal] is week: 2 of weekYear: 2016
For more info, see this similar Question: How to calculate Date from ISO8601 week number in Java.
WeekFields
In java.time you can also call upon the WeekFields
class, such as WeekFields.ISO.weekBasedYear()
. Should have the same effect as IsoFields
in later versions of Java 8 or later (some bugs were fixed in earlier versions of Java 8).
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
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
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 and 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 SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.