tl;dr
Use the handy YearWeek
class from ThreeTen-Extra library to represent an entire week. Then ask it to determine the date for any day-of-week within that week.
org.threeten.extra.YearWeek // Handy class representing a standard ISO 8601 week. Class found in the *ThreeTen-Extra* project, led by the same man as led JSR 310 and the *java.time* implementation.
.now( // Get the current week as seen in the wall-clock time used by the people of a certain region (a time zone).
ZoneId.of( "America/Chicago" )
) // Returns a `YearWeek` object.
.atDay( // Determine the date for a certain day within that week.
DayOfWeek.MONDAY // Use the `java.time.DayOfWeek` enum to specify which day-of-week.
) // Returns a `LocalDate` object.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone or offset-from-UTC.
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.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument. If critical, confirm the zone with your user.
Specify a proper time zone name in the format of Continent/Region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-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 ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
Or, better, use the Month
enum objects pre-defined, one for each month of the year. Tip: Use these Month
objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety. Ditto for Year
& YearMonth
.
LocalDate ld = LocalDate.of( 2014 , Month.APRIL , 6 ) ;
YearWeek
Your definition of a week running from Monday to Sunday matches that of the ISO 8601 standard.
Add the ThreeTen-Extra library to your project to access the YearWeek
class representing the standard week.
YearWeek week = YearWeek.from( ld ) ; // Determine the week of a certain date.
Or get today's week.
YearWeek week = YearWeek.now( z ) ;
Get the date for any day of the week. Specify which day by using DayOfWeek
enum.
LocalDate firstOfWeek = week.atDay( DayOfWeek.MONDAY ) ;
LocalDate lastOfWeek = week.atDay( DayOfWeek.SUNDAY ) ;