Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Calendar Hour of Day Returning 12 Hour Format

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

like image 703
Brian Leishman Avatar asked Aug 28 '14 01:08

Brian Leishman


3 Answers

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
like image 148
Evgeniy Dorofeev Avatar answered Nov 11 '22 13:11

Evgeniy Dorofeev


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

like image 13
MadProgrammer Avatar answered Nov 11 '22 12:11

MadProgrammer


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

like image 2
ankit Avatar answered Nov 11 '22 14:11

ankit