简体   繁体   中英

How to get the ISO Day of Week using Calendar API

I want to get the ISO Day of the Week using the Calendar API, but it returns the wrong day number.

Sunday is the last day of the week and should be number 7 from the ISO standard, with Monday being the first day as number 1.

But running the following code for a Sunday (2017-07-30) is returning me number 1 . Why is Calendar API returning me a different value?

Calendar cal = Calendar.getInstance(Locale.US);
cal.set(Calendar.YEAR, 2017);
cal.set(Calendar.MONTH, Calendar.JULY);
cal.set(Calendar.DAY_OF_MONTH, 30);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

System.out.println(dayOfWeek);

The result is: 1

I tried using Joda Time and it worked fine, but I need to do it using Calendar and Java 6, so using another library is not a solution.

tl;dr

Legacy way to determine day-of-week number defined ISO 8601 (Monday-Sunday, 1-7):

int dayOfWeekISO = cal.get( Calendar.DAY_OF_WEEK ) - 1 ;
if( dayOfWeekISO == 0 ) {  // For Sunday ( 1 - 1 = 0), add 7. 
    dayOfWeekISO = ( dayOfWeekISO + 1 ) ; 
}

Modern way:

LocalDate.of( 2017 , Month.JULY , 30 )  // Date-only value.
         .getDayOfWeek()                // `DayOfWeek.SUNDAY` enum object.
         .getValue()                    // 7 for Sunday. (1 = Monday, per ISO 8601 standard.)

7

Details

The accepted answer by Maia is correct: Those int constants on Calendar define the week as 1-7 for Sunday -Saturday, per the custom in the United States.

DAY_OF_WEEK

public static final int DAY_OF_WEEK

Field number for get and set indicating the day of the week. This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

If you absolutely cannot add a library, then you must adjust from the Calendar definition of a week being Sunday-Saturday 1-7. To put Money in the first place, subtract 1, then check for 0 to which you add 7.

int dayOfWeekISO = cal.get( Calendar.DAY_OF_WEEK ) - 1 ;
if( dayOfWeekISO == 0 ) {  // For Sunday ( 1 - 1 = 0), add 7. 
    dayOfWeekISO = ( dayOfWeekISO + 1 ) ; 
}

By the way to go the other way, from ISO 8601 style to US-style (Sunday first), calculate modulo 7 then plus 1.

( DayOfWeek.SUNDAY.getValue() % 7 ) + 1

Like this:

System.out.println( ( DayOfWeek.SUNDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.MONDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.TUESDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.WEDNESDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.THURSDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.FRIDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.SATURDAY.getValue() % 7 ) + 1 ) ;

1

2

3

4

5

6

7

But do consider adding a library of modern date-time classes . Well worth the bother of adding a jar to your project. Those old date-time classes really are wretched.

Avoid legacy date-time classes

The Calendar class is part of the troublesome old legacy date-time classes, now supplanted by java.time classes.

java.time

The java.time.DayOfWeek enum is standard, representing a week defined by ISO 8601 where a week runs from Monday to Sunday, numbered 1-7.

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

LocalDate ld = LocalDate.of( 2017 , Month.JULY , 30 ) ;
DayOfWeek dow = ld.getDayOfWeek() ;
int dowNumber = dow.getValue() ;
String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ; 

See this code run live at IdeOne.com .

ld.toString(): 2017-07-30

dow.toString(): SUNDAY

dowNumber: 7

output: Sunday


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 .

The int constants start with Sunday and end with Saturday.

Documented in java.util.Calendar JavaDoc for Java 6 .

So my proposal is to calculate it on your own.

Check the Javadoc of Calendar for java 6 before posting a question values of Constants in Calendar

The value of Calendar.SUNDAY is 1 for US Locale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM