TL; DR
LocalDate.now( ZoneId.of( "America/Montreal" ) ) .with( TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) ) // Specify your desired `DayOfWeek` as start-of-week. .atStartOfDay( ZoneId.of( "America/Montreal" ) )
See this code run on IdeOne.com .
zdt: 2017-07-09T00: 00-04: 00 [America / Montreal] | day of the week: SUNDAY
Avoid obsolete classes
You are using nasty old time classes that are now obsolete, being superseded by java.time classes.
DayOfWeek
Instead of using only integers to represent the day of the week in your code, use the DayOfWeek enum built into Java. This improves type safety, provides valid values, and makes your code more self-documenting.
DayOfWeek weekStart = DayOfWeek.SUNDAY ;
TemporalAdjuster and LocalDate
The TemporalAdjuster interface allows you to control the date to get a different date. Find some implementations in the TemporalAdjusters class (note the plural).
ZoneId z = ZoneId.of( "America/Montreal" ) ; LocalDate today = LocalDate.now( z ) ; LocalDate start = today.with( TemporalAdjusters.previousOrSame( weekStart ) ) ;
ZonedDateTime
To get the exact moment, ask LocalDate at the first moment of the day. This point depends on the time zone, as the date changes all over the world at any time.
ZonedDateTime zdt = start.atStartOfDay( z ) ;
Instant
If you want to view this moment, as in UTC, retrieve the Instant object.
Instant instant = zdt.toInstant() ;
Basil bourque
source share