9

In the Java docs, Calendar.HOUR is supposed to return the hour in the 12 hour format, and Calendar.HOUR_OF_DAY is supposed to return the hour in the 24 hour format, but both of these are returning in the 12 hour format.

My Code:

Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
System.out.println("hour: " + hour);

There is a question that is similar to mine already, but there's is for a specific time and I'm attempting to do this with the current time. That question is here java HOUR and HOUR_OF_DAY both returning 12-hr time


EDIT:

If it matters, this is happening within Eclipse on Windows, within cmd.exe on Windows, and Terminal on Ubuntu.


EDIT 2

Now I feel dumb... I didn't realize that I had multiple instances of calling the current time, and I was looking at the wrong one, which was HOUR_OF_DAY, but the one I was seeing in the console were being posted by just HOUR... Thanks for the help in the comments and the edit of my own post that led me to realize my mistake

Brian Leishman
  • 8,155
  • 11
  • 57
  • 93

6 Answers6

19

try this test

    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR, 17);
    System.out.println(c.get(Calendar.HOUR_OF_DAY));
    System.out.println(c.get(Calendar.HOUR));

it prints

17
5
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
14

When setting the hour, its important to either use HOUR_OF_DAY and 24 hour notation, or use HOUR and supply the AM_PM field...

Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 17);
System.out.println(c.get(Calendar.HOUR_OF_DAY));
System.out.println(c.get(Calendar.HOUR));
System.out.println(c.get(Calendar.AM_PM));

c.set(Calendar.HOUR_OF_DAY, 5);
System.out.println(c.get(Calendar.HOUR_OF_DAY));
System.out.println(c.get(Calendar.HOUR));
System.out.println(c.get(Calendar.AM_PM));

Will print...

17
5
1 // PM
5
5
0 // AM

When I use

c.set(Calendar.HOUR, 17);
System.out.println(c.get(Calendar.HOUR_OF_DAY));
System.out.println(c.get(Calendar.HOUR));
System.out.println(c.get(Calendar.AM_PM));

I get...

5
5
0 // AM

Which means the API has filtered the result and made an internal correction. It's VERY, important to use the right field for the right value as the Calendar can roll values as it sees fit...

If I add c.setLenient(false);, it will throw a java.lang.IllegalArgumentException: HOUR because 17 is not a valid value for HOUR

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • So how should I modify my current code to have HOUR_OF_DAY return correctly for the current time of day? – Brian Leishman Aug 28 '14 at 02:27
  • 1
    That depends, if you're using a hour value of +12, it will work. If you're using values between 1-12, then you need to set the `AM_PM` value accordingly...If you say the "hour is 5" what does that actually mean? The `Calendar` has only a primitive concept of what the actually means... – MadProgrammer Aug 28 '14 at 02:29
  • In your current code, I would also inspect the `AM_PM` flag and see what's value is... – MadProgrammer Aug 28 '14 at 02:30
  • I got it working... It was a whole different problem, but thanks for the help, your answer did ultimately lead me to my conclusion – Brian Leishman Aug 28 '14 at 02:35
  • 1
    Good, cause I was confused :P – MadProgrammer Aug 28 '14 at 02:36
2

if you are running code at server side then stop server and then delete project from server and clean server after that your problem solved.

but if not then create a Test class:

public class Test {

public static void main(String[] args) {
    Calendar calender = Calendar.getInstance();

    System.out.println(calender.getTimeInMillis());

    calender.set(Calendar.HOUR, 2);

    System.out.println(calender.getTimeInMillis());

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

System.out.println(calender.get(Calendar.HOUR_OF_DAY));
    System.out.println(calender.get(Calendar.HOUR));
        Calendar calender1 = Calendar.getInstance();
        System.out.println(calender1.getTimeInMillis());
        calender1.setTimeInMillis(calender.getTimeInMillis());

    System.out.println(calender1.getTimeInMillis());

    System.out.println(calender1.getTimeInMillis());

}}

then right click on class in eclipse and run as java application. then it works

ankit
  • 2,591
  • 2
  • 29
  • 54
1

I tried your source.

It can get right result.

devbetter
  • 21
  • 2
0

Checking all the areas in my code that referenced the Calendar object to try to get the hours was the problem. I did this in three different locations but only modified one of the three, which is why my updates didn't seem to take effect

Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
0

tl;dr

Instant.now()
       .atZone( ZoneId.of( "America/New_York" ) )
       .getHour()

Using java.time

You are using troublesome old legacy date-time classes now supplanted by the java.time classes.

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

Get current moment:

Instant instant = Instant.now();

instant.toString(): 2016-09-16T20:46:01.123456789Z

ZonedDateTime

Time zone is crucial in determining the date and time-of-day. For any given moment, the date and the time-of-day vary around the globe by zone.

Apply a time zone to see some region’s wall-clock time.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

zdt.toString() 2016-09-16T16:46:01.123456789-04:00[America/Montreal]

Interrogate for time-of-day as a number 0-23.

int hourOfDay = zdt.gotHour();

16

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154