Abhijeet Panwar
Abhijeet Panwar

Reputation: 1867

Calender.HOUR_OF_DAY returns wrong results

I have written a code to get value of hour from a calender object , but it returns wrong result.

mpleDateFormat sf = new SimpleDateFormat("hh:mm");

    try {
        time = sf.parse("09:00");
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(time);

    System.out.println(cal);
    System.out.println(cal.HOUR_OF_DAY);

In First statement calender's toString method shows value 9 for Hour_OF_DAY , but cal.HOUR_OF_DAY prints 11.

Upvotes: 2

Views: 1516

Answers (1)

Keppil
Keppil

Reputation: 46219

Calendar.HOUR_OF_DAY is a constant (with the value 11) meant to be used together with the Calendar#get() method to retrieve the hour value:

System.out.println(cal.get(Calendar.HOUR_OF_DAY));

This prints

9

Upvotes: 2

Related Questions