Get current week start and end date in Java - (MONDAY TO SUNDAY)
Updated answer using Java 8
Using Java 8 and keeping the same principle as before (the first day of the week depends on your Locale
), you should consider using the following:
Obtain the first and last DayOfWeek
for a specific Locale
final DayOfWeek firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();
final DayOfWeek lastDayOfWeek = DayOfWeek.of(((firstDayOfWeek.getValue() + 5) % DayOfWeek.values().length) + 1);
Query for this week's first and last day
LocalDate.now(/* tz */).with(TemporalAdjusters.previousOrSame(firstDayOfWeek)); // first day
LocalDate.now(/* tz */).with(TemporalAdjusters.nextOrSame(lastDayOfWeek)); // last day
Demonstration
Consider the following class
:
private static class ThisLocalizedWeek {
// Try and always specify the time zone you're working with
private final static ZoneId TZ = ZoneId.of("Pacific/Auckland");
private final Locale locale;
private final DayOfWeek firstDayOfWeek;
private final DayOfWeek lastDayOfWeek;
public ThisLocalizedWeek(final Locale locale) {
this.locale = locale;
this.firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();
this.lastDayOfWeek = DayOfWeek.of(((this.firstDayOfWeek.getValue() + 5) % DayOfWeek.values().length) + 1);
}
public LocalDate getFirstDay() {
return LocalDate.now(TZ).with(TemporalAdjusters.previousOrSame(this.firstDayOfWeek));
}
public LocalDate getLastDay() {
return LocalDate.now(TZ).with(TemporalAdjusters.nextOrSame(this.lastDayOfWeek));
}
@Override
public String toString() {
return String.format( "The %s week starts on %s and ends on %s",
this.locale.getDisplayName(),
this.firstDayOfWeek,
this.lastDayOfWeek);
}
}
We can demonstrate its usage as follows:
final ThisLocalizedWeek usWeek = new ThisLocalizedWeek(Locale.US);
System.out.println(usWeek);
// The English (United States) week starts on SUNDAY and ends on SATURDAY
System.out.println(usWeek.getFirstDay()); // 2018-01-14
System.out.println(usWeek.getLastDay()); // 2018-01-20
final ThisLocalizedWeek frenchWeek = new ThisLocalizedWeek(Locale.FRANCE);
System.out.println(frenchWeek);
// The French (France) week starts on MONDAY and ends on SUNDAY
System.out.println(frenchWeek.getFirstDay()); // 2018-01-15
System.out.println(frenchWeek.getLastDay()); // 2018-01-21
Original Java 7 answer (outdated)
Simply use:
c.setFirstDayOfWeek(Calendar.MONDAY);
Explanation:
Right now, your first day of week is set on Calendar.SUNDAY
. This is a setting that depends on your Locale
.
Thus, a better alternative would be to initialise your Calendar
specifying the Locale
you're interested in.
For example:
Calendar c = GregorianCalendar.getInstance(Locale.US);
... would give you your current output, while:
Calendar c = GregorianCalendar.getInstance(Locale.FRANCE);
... would give you your expected output.
Well, looks like you got your answer. Here's an add-on, using java.time in Java 8 and later. (See Tutorial)
import java.time.DayOfWeek;
import java.time.LocalDate;
public class MondaySunday
{
public static void main(String[] args)
{
LocalDate today = LocalDate.now();
// Go backward to get Monday
LocalDate monday = today;
while (monday.getDayOfWeek() != DayOfWeek.MONDAY)
{
monday = monday.minusDays(1);
}
// Go forward to get Sunday
LocalDate sunday = today;
while (sunday.getDayOfWeek() != DayOfWeek.SUNDAY)
{
sunday = sunday.plusDays(1);
}
System.out.println("Today: " + today);
System.out.println("Monday of the Week: " + monday);
System.out.println("Sunday of the Week: " + sunday);
}
}
Another way of doing it, using temporal adjusters.
import java.time.LocalDate;
import static java.time.DayOfWeek.MONDAY;
import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.nextOrSame;
import static java.time.temporal.TemporalAdjusters.previousOrSame;
public class MondaySunday
{
public static void main(String[] args)
{
LocalDate today = LocalDate.now();
LocalDate monday = today.with(previousOrSame(MONDAY));
LocalDate sunday = today.with(nextOrSame(SUNDAY));
System.out.println("Today: " + today);
System.out.println("Monday of the Week: " + monday);
System.out.println("Sunday of the Week: " + sunday);
}
}