tl;dr
YearWeek.from( // Represents week of standard ISO 8601 defined week-based-year (as opposed to a calendar year).
LocalDate.parse( "2017-01-23" ) // Represents a date-only value, without time-of-day and without time zone.
) // Returns a `YearWeek` object.
.getWeek() // Or, `.getYear()`. Both methods an integer number.
4
ISO 8601 standard week
If you want the standard ISO 8601 week, rather than a localized definition of a week, use the YearWeek
class found in the ThreeTen-Extra project that adds functionality to the java.time classes built into Java 8 and later.
ISO-8601 defines the week as always starting with Monday. The first week is the week which contains the first Thursday of the calendar year. As such, the week-based-year used in this class does not align with the calendar year.
First, get today's date. The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Or let the user specify a date by typing a string. Parsing string input of a date is covered in many other Questions and Answers. Simplest is to have the user use standard ISO 8601 format, YYYY-MM-DD such as 2017-01-23
.
LocalDate ld = LocalDate.parse( "2017-01-23" ) ;
For other formats, specify a DateTimeFormatter
for parsing. Search Stack Overflow for many many examples of using that class.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "d/M/uuuu" , Locale.US ) ;
LocalDate ld = LocalDate.parse( "1/23/2017" , f ) ;
Get the YearWeek
.
YearWeek yw = YearWeek.from( ld ) ;
To create a string, consider using the standard ISO 8601 format for year-week, yyyy-Www such as 2017-W45
. Or you can extract each number.
YearWeek::getWeek
– Gets the week-of-week-based-year field.
YearWeek::getYear
– Gets the week-based-year field.
Other definitions of week
The above discussion assumes you go by the ISO 8601 definition of weeks and week-numbering. If instead you want an alternate definition of week and week-numbering, see the Answer by Mobolaji D. using a locale’s definition.
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
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
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.