Skip to content

Instantly share code, notes, and snippets.

@Longwater1234
Created October 27, 2021 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Longwater1234/5180e8c0ea8749ddafe04dbf16dd8d76 to your computer and use it in GitHub Desktop.
Save Longwater1234/5180e8c0ea8749ddafe04dbf16dd8d76 to your computer and use it in GitHub Desktop.
Given a date, returns first day of that Week.
/**
* FOrces Sunday as first day of week.
*
* @param date given date
* @return First day of this day's week
*/
public static Date getFirstDayofWeek(@NotNull Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int currentDay = cal.get(Calendar.DAY_OF_WEEK);
int pastDays = Calendar.SUNDAY - currentDay;
cal.add(Calendar.DATE, pastDays);
return cal.getTime();
}
/**
* Sunday is default first day of Week
*
* @param dateString String value of Date
* @return first day of that week
*/
public static LocalDate getLocalFirstDayWeek(@NotNull String dateString) {
LocalDate thatDay = LocalDate.parse(dateString);
int pastDays = thatDay.getDayOfWeek().getValue();
return thatDay.minusDays(pastDays);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment