Hour of the day Java Calendar Return 12 hour format

In Java docs, Calendar.HOUR

should return hour in 12 hour format, and Calendar.HOUR_OF_DAY

should return hour in 24 hour format, but they both return in 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 already similar to mine, but there is a specific time and I am trying to do it with the current time. See this question here java HOUR and HOUR_OF_DAY both return 12 hour time


EDIT:

If it matters, it happens in Eclipse on Windows, cmd.exe on Windows, and Terminal on Ubuntu.


EDIT 2

Now I feel stupid ... I didn't realize I had multiple instances of the current time call and I was looking not for the one that was HOUR_OF_DAY, but the one I saw on the console was only posted by HOUR ... Thanks for help in the comments and editing my own post which led me to realize my mistake.

+3


source to share


6 answers


Checking all scopes in my code that referenced the Calendar object to try and get the hours was a problem. I did this in three different places, but only changed one of the three, so my updates didn't seem to take effect.



0


source


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

      



he prints

17
5

      

+12


source


When setting the hour, it is important to use HOUR_OF_DAY

both 24-hour notation or use HOUR

and put the field AM_PM

...

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 be printed ...

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

      

This means that the API has filtered the result and made an internal correction. It is VERY important to use the correct field for the correct value as it Calendar

can roll the values ​​at will ...

If I add c.setLenient(false);

it will throw out java.lang.IllegalArgumentException: HOUR

because it is 17

not a valid value forHOUR

+7


source


I tried your source.

He can get the right result.

+1


source


if you are using server side code then stop the server and then delete the project from the server and clean up the server after which your problem is 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 the class in eclipse and run it as java application. then it works

+1


source


TL; DR

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

      

Using java.time

You are using the nasty old legacy time classes that are now being supplanted by the java.time classes.

Instant

Instant

the class represents a moment on the timeline in UTC with nanosecond resolution .

Get the current moment:

Instant instant = Instant.now();

      

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

ZonedDateTime

The time zone is critical to determining the date and time of day. At any given time and time of day, zones change around the world.

Apply a time zone to see some area of the 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]

Poll the time of day as the number 0-23.

int hourOfDay = zdt.gotHour();

      

sixteen

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the annoying old time classes such as java.util.Date

, .Calendar

and java.text.SimpleDateFormat

.

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

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

Most of the java.time functionality goes back to Java 6 and 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 proof of possible future additions to java.time. Here you can find useful classes, such as Interval

, YearWeek

, YearQuarter

, etc.

0


source







All Articles