Java 日历小时返回 12 小时格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25539540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Calendar Hour of Day Returning 12 Hour Format
提问by Brian Leishman
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,但是我在控制台中看到的那个是由 just小时......感谢评论中的帮助和我自己帖子的编辑,这让我意识到了我的错误
采纳答案by MadProgrammer
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. It's VERY, important to use the right field for the right value as the Calendar
can roll values as it sees fit...
这意味着 API 已经过滤了结果并进行了内部更正。将正确的字段用于正确的值非常重要,Calendar
因为它认为合适的可以滚动值......
If I add c.setLenient(false);
, it will throw a java.lang.IllegalArgumentException: HOUR
because 17
is not a valid value for HOUR
如果我添加c.setLenient(false);
,它会抛出一个java.lang.IllegalArgumentException: HOUR
因为17
不是有效值HOUR
回答by Evgeniy Dorofeev
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
回答by devbetter
I tried your source.
我试过你的来源。
It can get right result.
它可以得到正确的结果。
回答by Brian Leishman
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
检查我的代码中引用 Calendar 对象的所有区域以尝试获取小时数是问题所在。我在三个不同的地方做了这个,但只修改了三个中的一个,这就是为什么我的更新似乎没有生效
回答by Basil Bourque
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
Instant
The Instant
class represents a moment on the timeline in UTCwith a resolution of nanoseconds.
Get current moment:
获取当前时刻:
Instant instant = Instant.now();
instant.toString(): 2016-09-16T20:46:01.123456789Z
Instant.toString(): 2016-09-16T20:46:01.123456789Z
ZonedDateTime
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.timeframework 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
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧日期时间类,例如java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to java.time.
现在处于维护模式的Joda-Time项目建议迁移到 java.time。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backportand further adapted to Androidin ThreeTenABP(see How to use…).
大部分的java.time功能后移植到Java 6和7 ThreeTen,反向移植,并进一步用于安卓在ThreeTenABP(见如何使用......)。
The ThreeTen-Extraproject 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.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,如Interval
,YearWeek
,YearQuarter
,等。
回答by ankit
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. then it works
然后右键单击eclipse中的类并作为java应用程序运行。然后它起作用了