Gregorian Calendar starting day of the week Tuesday instead of Sunday

757 views Asked by At

I am using GregorianCalendar as it follows

new GregorianCalendar(1900, Calendar.JANUARY, 1)

and returns a date starting from Tuesday.

Is there any way to configure it in order to return as first day of the week the 'Sunday'.

I want to return a Calendar object.

I used the following solution

switch (gregorianCalendar.get(Calendar.DAY_OF_WEEK)) {    
        case Calendar.MONDAY:
            gregorianCalendar.add(Calendar.DAY_OF_WEEK, -1);    
            break;  
        case Calendar.TUESDAY:  
            gregorianCalendar.add(Calendar.DAY_OF_WEEK, -2);  
            break;  
        case Calendar.WEDNESDAY:  
            gregorianCalendar.add(Calendar.DAY_OF_WEEK, -3);  
            break;  
        case Calendar.THURSDAY:  
            gregorianCalendar.add(Calendar.DAY_OF_WEEK, -4);  
            break;  
        case Calendar.FRIDAY:  
            gregorianCalendar.add(Calendar.DAY_OF_WEEK, -5);  
            break;  
        case Calendar.SATURDAY:    
            gregorianCalendar.add(Calendar.DAY_OF_WEEK, -6);  
            break;  
    }

I don't know if its the best one.

2

There are 2 answers

0
user2708647 On

I understand you are looking for previous Sunday ? Then I would rather do something like:

while (gregorianCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) { gregorianCalendar.add(Calendar.DAY_OF_WEEK, -1); }

0
Basil Bourque On

tl;dr

LocalDate.of( 2017 , Month.MARCH , 23 )
         .with( TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) )

java.time

The modern way to determine the date of a previous day-of-week is with the java.time classes. The old date-time classes such as Date and Calendar are now legacy, obsolete.

The LocalDate class represents a date-only value without time-of-day and without time zone.

The TemporalAdjuster interface provides a way to determine other dates from one date. Implementations we need are found in the TemporalAdjusters class (note plural).

LocalDate ld = LocalDate.of( 2017 , Month.MARCH , 23 ) ;
LocalDate previousSunday = ld.with( TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY ) ) ;

If you are trying to represent a week as a collection of day-of-week values in a particular order, use an EnumSet with DayOfWeek enum objects.

EnumSet<DayOfWeek> dows = EnumSet.of( DayOfWeek.SUNDAY , DayOfWeek.MONDAY , … ) ;

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?

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.