简体   繁体   English

Java日历时段返回12小时格式

[英]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. 在Java文档中, Calendar.HOUR应该以12小时格式返回小时,而Calendar.HOUR_OF_DAY应该以24小时格式返回小时,但这两种格式都以12小时格式返回。

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 这个问题在这里java HOUR和HOUR_OF_DAY都返回12小时的时间


EDIT: 编辑:

If it matters, this is happening within Eclipse on Windows, within cmd.exe on Windows, and Terminal on Ubuntu. 如果重要,这将发生在Windows上的Eclipse,Windows上的cmd.exe和Ubuntu上的终端中。


EDIT 2 编辑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 现在我感到愚蠢......我没有意识到我有多个调用当前时间的实例,而我正在查看错误的一个,这是HOUR_OF_DAY,但是我在控制台中看到的那个被发布了小时...感谢您在评论中的帮助和我自己的帖子编辑,这让我意识到自己的错误

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

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... 设置小时时,使用HOUR_OF_DAY和24小时表示法很重要,或者使用HOUR并提供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 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. 这意味着API已过滤结果并进行内部更正。 It's VERY, important to use the right field for the right value as the Calendar can roll values as it sees fit... 将正确的字段用于正确的值是非常重要的,因为Calendar可以按其认为合适的方式滚动值...

If I add c.setLenient(false); 如果我添加c.setLenient(false); , it will throw a java.lang.IllegalArgumentException: HOUR because 17 is not a valid value for HOUR ,它会抛出一个java.lang.IllegalArgumentException: HOUR因为17不是HOUR的有效值

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: 但如果没有,那么创建一个Test类:

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. 然后右键单击eclipse中的类并作为java应用程序运行。 then it works 然后它工作

I tried your source. 我试过你的来源。

It can get right result. 它可以得到正确的结果。

tl;dr TL;博士

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

Using java.time 使用java.time

You are using troublesome old legacy date-time classes now supplanted by the java.time classes. 您正在使用现在由java.time类取代的麻烦的旧遗留日期时间类。

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds . Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒

Get current moment: 获取当前时刻:

Instant instant = Instant.now();

instant.toString(): 2016-09-16T20:46:01.123456789Z 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] zdt.toString()2016-09-16T16:46:01.123456789-04:00 [美国/蒙特利尔]

Interrogate for time-of-day as a number 0-23. 以0-23的数字询问时间。

int hourOfDay = zdt.gotHour();

16 16

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old date-time classes such as java.util.Date , .Calendar , & java.text.SimpleDateFormat . 这些类取代了麻烦的旧日期时间类,如java.util.Date.Calendarjava.text.SimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to java.time. 现在处于维护模式Joda-Time项目建议迁移到java.time。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。

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… ). 大部分的java.time功能后移植到Java 6和7 ThreeTen,反向移植 ,并进一步适应的AndroidThreeTenABP (见如何使用...... )。

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more. 您可以在这里找到一些有用的课程,如IntervalYearWeekYearQuarter等。

Checking all the areas in my code that referenced the Calendar object to try to get the hours was the problem. 检查我的代码中引用Calendar对象的所有区域以尝试获取小时数是问题所在。 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 我在三个不同的位置做了这个,但只修改了三个中的一个,这就是为什么我的更新似乎没有生效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM