Java Calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY), will it roll backwards, forwards or unknown?
It should always keep the same WEEK_OF_MONTH
(http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html#WEEK_OF_MONTH). From the documentation:
When setting or getting the WEEK_OF_MONTH or WEEK_OF_YEAR fields, Calendar must determine the first week of the month or year as a reference point. The first week of a month or year is defined as the earliest seven day period beginning on getFirstDayOfWeek() and containing at least getMinimalDaysInFirstWeek() days of that month or year. Weeks numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow it. Note that the normalized numbering returned by get() may be different. For example, a specific Calendar subclass may designate the week before week 1 of a year as week n of the previous year.
the following formula returns "current" day in a week in range of [0;6]
(d + numberOfDaysInAWeek - firstDayOfWeek) % numberOfDaysInAWeek
or add 1 if you would like range [1;7]
(d + numberOfDaysInAWeek - firstDayOfWeek) % numberOfDaysInAWeek + 1
d
is what Calendar.get(Calendar.DAY_OF_WEEK)
returns
to get first day of a week, subtract formula's result from current date. The following code does it:
final int currentDayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 7 - cal.getFirstDayOfWeek()) % 7;
cal.add(Calendar.DAY_OF_YEAR, -currentDayOfWeek);
Using java.time
The modern approach is with the java.time classes that supplant the troublesome old legacy date-time classes.
The java.time classes are much easier to work with. In particular, they remove the ambiguity raised in the Question. You can explicitly ask either for the earlier Sunday or for the later Sunday.
The LocalDate
class represents a date-only value without time-of-day and without time zone.
The Month
enum provides a dozen pre-defined objects, one for each month of the year. These enum objects are safer to use, but you can instead use a plain number for the month. Unlike the legacy classes, these months have sane numbering, 1-12 for January-December.
LocalDate localDate = LocalDate.of( 2009 , Month.AUGUST, 22 );
The TemporalAdjuster
interface provides for manipulation of date-time values. The TemporalAdjusters
class (note the plural s
) provides several handy implementations.
The previous
& next
adjusters exclude the date itself from consideration. The previousOrSame
& nextOrSame
methods return the date in question if it is indeed the desired day-of-week.
The DayOfWeek
enum provides seven pre-defined objects, one for each day of the week.
LocalDate previousSunday = localDate.with( TemporalAdjusters.previous ( DayOfWeek.SUNDAY ));
LocalDate previousOrSameSunday = localDate.with( TemporalAdjusters.previousOrSame ( DayOfWeek.SUNDAY ));
LocalDate nextSunday = localDate.with( TemporalAdjusters.next ( DayOfWeek.SUNDAY ));
LocalDate nextOrSameSunday = localDate.with( TemporalAdjusters.nextOrSame ( DayOfWeek.SUNDAY ));
Dump to console.
System.out.println ("localDate: " + localDate + " ( " + localDate.getDayOfWeek ().getDisplayName ( TextStyle.FULL, Locale.US ) + " )");
System.out.println ("previousSunday: " + previousSunday );
System.out.println ("nextSunday: " + nextSunday );
localDate: 2009-08-22 ( Saturday )
previousSunday: 2009-08-16
nextSunday: 2009-08-23
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
-
Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
-
Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
-
Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
It depends, actually. Consider the following Java code. It is actually quite simple and I expect it to print the monday preceding 2011-09-18, that is 2011-09-12:
Calendar calendar = Calendar.getInstance(Locale.GERMANY);
System.out.printf("First day of week: %d%n%n", calendar.getFirstDayOfWeek());
calendar.set(2011, Calendar.SEPTEMBER, 18);
System.out.printf("Starting day: %tF%n", calendar);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.printf("Last monday: %tF%n%n", calendar);
calendar.set(2011, Calendar.SEPTEMBER, 18);
System.out.printf("Starting day: %tF (week %d)%n",
calendar, calendar.get(Calendar.WEEK_OF_YEAR));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.printf("Last monday: %tF (week %d)%n", calendar,
calendar.get(Calendar.WEEK_OF_YEAR));
But in fact the result is a bit different:
First day of week: 2
Starting day: 2011-09-18
Last monday: 2011-09-19
Starting day: 2011-09-18 (week 37)
Last monday: 2011-09-12 (week 37)
In other words, the result depends on whether my calendar knows that I might be interested in the week. The result actually changes if I query WEEK_OF_YEAR
!