Get the start date of the week (custom start day of the week)

I have a current date and a constant that tells which day the week starts. I want to get the start date of the week based on this constant. If I rigidly set the first day of the week until Monday (or something else), then it's simple. But the first day of the week continues to change. Therefore, I do not want to change the code every time I need to change the first day.

This is what I tried using java Calendar:

public static Date getLastWeekdayDate() { Calendar calendar = new GregorianCalendar(); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); int daysToSubtractFromCurrentDate = 0; switch (dayOfWeek) { case Calendar.WEDNESDAY: daysToSubtractFromCurrentDate = 4; break; case Calendar.THURSDAY: daysToSubtractFromCurrentDate = 5; break; case Calendar.FRIDAY: daysToSubtractFromCurrentDate = 6; break; case Calendar.SATURDAY: daysToSubtractFromCurrentDate = 0; break; case Calendar.SUNDAY: daysToSubtractFromCurrentDate = 1; break; case Calendar.MONDAY: daysToSubtractFromCurrentDate = 2; break; case Calendar.TUESDAY: daysToSubtractFromCurrentDate = 3; break; } calendar.add(Calendar.DATE, -daysToSubtractFromCurrentDate); calendar.set(Calendar.AM_PM, Calendar.AM); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } 

I want to get the start date of the week. The above function returns the first day of the week, and the day of the beginning of the week is hard-coded on Saturday. Whenever a requirement changes, starting from the day the week starts, I have to change the code.

+3
java
source share
3 answers

I used the following method:

 /** 1 = Sunday, 2 = Monday, 3 = Tuesday, 4 = Wednesday, 5 = Thursday, * 6 = Friday, 7 = Saturday */ public static Date getFirstDayOfWeekDate(int firstDay) { // Calculate the date of the first day of the week // First get the today date Calendar c = new GregorianCalendar(); // Now set the day of week to the first day of week while (c.get(Calendar.DAY_OF_WEEK) != firstDay) { c.add(Calendar.DAY_OF_MONTH, -1); } return c.getTime(); } 
+1
source share

From the java calendar API http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#getFirstDayOfWeek ()

 public int getFirstDayOfWeek() Gets what the first day of the week is; eg, SUNDAY in the US, MONDAY in France. Returns: the first day of the week. See Also: 
+2
source share

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 ; // Pass whatever `DayOfWeek` object you want. 

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() ; // Same moment, different wall-clock time. 
0
source share

All Articles