Created
October 27, 2021 15:03
-
-
Save Longwater1234/5180e8c0ea8749ddafe04dbf16dd8d76 to your computer and use it in GitHub Desktop.
Given a date, returns first day of that Week.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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