在 Java 中询问公历以获取一天中的小时数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2702971/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:25:32  来源:igfitidea点击:

Ask Gregorian Calendar for hour of day in java

javadatetimegregorian-calendarhourminute

提问by Leanne C

I'm using a Gregorian Calendar to set a specific date and time to an application using the set function of the Gregorian Calendar. When i use the getTime() method, it gives me the right output however when i try to access the Hour_Of_Day and Minute it gives a wrong number.

我正在使用公历使用公历的设置功能为应用程序设置特定的日期和时间。当我使用 getTime() 方法时,它为我提供了正确的输出,但是当我尝试访问 Hour_Of_Day 和 Minute 时,它​​给出了错误的数字。

    Calendar time = new GregorianCalendar();
    time.set(2010, Calendar.JANUARY, 1, 7, 20,0);       
    hour = time.HOUR_OF_DAY;
    minute = time.MINUTE; 

The hour gives an output of 11 and the minute gives an a value of 12.
Any suggestions on how to fix this? Thanks

小时的输出值为 11,分钟的输出值为 12。
有关如何解决此问题的任何建议?谢谢

回答by Brett Kail

Your code is just assigning hour/minute to constants. You need to call Calendar.get(int):

您的代码只是将小时/分钟分配给常量。您需要调用 Calendar.get(int):

hour = time.get(Calendar.HOUR_OF_DAY);
minute = time.get(Calendar.MINUTE);

回答by Basil Bourque

tl;dr

tl;博士

myGregCal
    .toZonedDateTime()  // Convert from legacy class GregorianCalendar to modern ZonedDateTime.
    .getHour()          // Get hour-of-day, 0-23.

java.time

时间

Much easier with the modern java.time classes that replace those troublesome old legacy date-time classes.

使用现代 java.time 类替换那些麻烦的旧的遗留日期时间类要容易得多。

The ZonedDateTimeclass represents a moment on the timeline in a specific time zone with a resolution of nanoseconds.

ZonedDateTime类代表特定时区的时间线与纳秒的分辨率上一会儿。

Convert from your GregorianCalendarusing new methods added to the old classes.

从您GregorianCalendar使用添加到旧类的新方法进行转换。

ZonedDateTime zdt = myGregCal.toZonedDateTime() ;

Or start fresh without the GregorianCalendarclass.

或者在没有GregorianCalendar课的情况下重新开始。

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.of( 2010 , Month.JANUARY , 1 , 7 , 20 , 0 , 0 , z );

If you want to work with just the time-of-day portion, extract a LocalTime.

如果您只想处理时间部分,请提取LocalTime.

LocalTime localTime = zdt.toLocalTime() ;

If you really want the integer numbers of hours and minutes, you can interrogate for those.

如果你真的想要小时和分钟的整数,你可以查询这些。

int hour = zdt.getHour();
int minute = zdt.getMinute();


About java.time

关于 java.time

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

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

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

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?

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 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多