简体   繁体   中英

Not displaying day of the week using calendar and date instances

I have created new SimpleDateFormat object which parses the given string as date object. The date format is as below:

SimpleDateFormat simpledateFormat = new SimpleDateFormat("dd-MM-yyyy");

And I am setting this date to calendar instance as below:

Date date = sampledateFormat.parse("01-08-2013");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

Now I am getting the day of the day of the week from this calendar. It is giving wrong value.

System.out.println(calendar.DAY_OF_WEEK);

The output it is giving is 7 ie Saturday but the expected value is 5 ie Thursday. Whats the problem?

You should print

calendar.get(Calendar.DAY_OF_WEEK);

The Calendar class has DAY_OF_WEEK as integer constant (with value 7) which should be used in conjunction with the Calendar.get(int) method. DAY_OF_WEEK is a calendar field, and all these constant fields are used to get() different values from the calendar instance. Their value is irrelevant.

tl;dr

LocalDate.parse(    // Parse the input string by specified formatting pattern to get a date-only `LocalDate` object.
    "01-08-2013" , 
    DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)
.getDayOfWeek()    // Get a `DayOfWeek` enum object. This is *not* a mere String.
.getValue()        // Ask the `DayOfWeek` object for its number, 1-7 for Monday-Sunday per ISO 8601 standard.

4

java.time

The modern approach uses the java.time classes that supplanted the troublesome old legacy date-time classes such as SimpleDateFormat and Date and Calendar .

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

Define a formatting pattern to match.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ;

Parse the input string.

LocalDate ld = LocalDate.parse( "01-08-2013" , f ) ;

ld.toString(): 2013-08-01

Interrogate for the day-of-week. Get a DayOfWeek enum object, one of seven pre-defined objects, for Monday-Sunday.

DayOfWeek dow = ld.getDayOfWeek() ;

dow.toString(): THURSDAY

You can ask that DayOfWeek object for a localized name and for a number 1-7 for Monday-Sunday per the ISO 8601 standard.

int dowNumber = dow.getValue() ; 

4

String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;  // Or Locale.US, Locale.ITALY, etc.

jeudi


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 .

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

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 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