日历类问题 [英] Calendar Class problems
问题描述
我在日历
类上遇到问题。
日历cal = Calendar.getInstance();
int iYear = cal.get(Calendar.YEAR); //获取当前年份
int iMonth = cal.get(Calendar.MONTH); // month ...
int iDay = cal.get(日历); //本月的当前日期
这...没有工作! :-(
我使用调试器,发现YEAR和DAY_OF_MONTH是正确的,但是
应该是MONTH为1(一月)
在这里它变得更奇怪:
然后我尝试了 cal.clear();
后跟 cal.set(2014,2,27); //今天的日期-2014年2月27日
并且该月仍为1(即1月)
我将日期设置为1月的天数(2014,1,1),(2014,1,16),等等
它正确地给我一个月1 b
$ b
在阅读并尝试了很多东西之后(然后拔出头发)。
我将其设置为将来的某个日期,生日(2014、5、23)等
对于那些日期,Month正确设置为5(五月)
tl; dr
LocalDate.now()
.getYear()
java.time
现代方法u使用业界领先的 java.time 类。
LocalDate
LocalDate
类表示没有日期和时区的仅日期值。
时区对于确定日期至关重要。在任何给定时刻,日期都会在全球范围内变化。例如,在法国巴黎午夜过后的几分钟,虽然仍然是昨天中魁北克省。
如果未指定时区,则JVM隐式应用其当前的默认时区。该默认值可能随时更改,因此您的结果可能会有所不同。最好将所需的/期望的时区明确指定为参数。
指定正确的时区名称,格式为大陆/地区
,例如 America / Montreal
, Africa / Casablanca
或 Pacific / Auckland
。请勿使用3-4个字母的缩写,例如 EST
或 IST
,因为它们不是 真实时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( America / Montreal);
今天的LocalDate = LocalDate.now(z);
如果要使用JVM的当前默认时区,请提出要求并作为参数传递。如果省略,则会隐式应用JVM的当前默认值。
ZoneId z = ZoneId.systemDefault(); //获取JVM的当前默认时区。
或指定日期。您可以用数字设置月份,一月至十二月的理智编号为1-12。
LocalDate ld = LocalDate.of(1986,2,23); //年使用理智的直接编号(1986年表示1986年)。月使用理智的编号,1月至12月为1到12。
或者更好的方法是使用 Month
枚举对象已预先定义,每个对象一个一年中的月份。提示:在整个代码库中使用这些 Month
对象,而不是仅使用整数,可以使您的代码更具自记录性,确保值有效,并提供类型安全。
LocalDate ld = LocalDate.of(1986,Month.FEBRUARY,23);
零件
将零件询价为
int dayOfMonth = ld.getDayOfMonth();
int month = ld.getMonthValue();
int year = ld.getYear();
关于java.time
java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date
, Calendar
,& SimpleDateFormat
。
现在处于维护模式的Joda-Time 项目建议迁移到 java.time 类。
要了解更多信息,请参见 Oracle教程。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310 。
从哪里获取java.time类?
- Java SE 8 , Java SE 9 ,然后是
- 内置。
- 具有捆绑实现的标准Java API的一部分。
- Java 9添加了一些次要功能和修复。
- Java SE 6 和 Java SE 7
- 许多java.time功能都反向移植到Java 6& 7在> ThreeTen-Backport 中。
- Android
- 较新版本的java.time类的Android捆绑实现。
- 对于早期的Android, ThreeTenABP 项目改编了 ThreeTen-Backport (如上所述)。请参阅 如何使用ThreeTenABP… 。
ThreeTen-Extra 项目使用其他类扩展了java.time。该项目是将来可能向java.time添加内容的试验场。您可能会在这里找到一些有用的类,例如 时间间隔
, YearWeek
, YearQuarter
和更多。
I am having problems with the Calendar
Class.
Calendar cal = Calendar.getInstance ();
int iYear = cal.get (Calendar.YEAR); // get the current year
int iMonth = cal.get (Calendar.MONTH); // month...
int iDay = cal.get (Calendar.); // current day in the month
This... No Workie!! :-(
I used the debugger and found that the YEAR and the DAY_OF_MONTH are correct, however, the MONTH is 1 (January) when it SHOULD BE 2 (February).
Here is where it gets even more WEIRD:
I then tried cal.clear ();
followed by cal.set (2014, 2, 27); // Today's Date - Feb 27, 2014
and the month was still 1 (i.e. January)
I set the date to days in January, (2014, 1, 1), (2014, 1, 16),etc It correctly gave me a 1 for the month
After reading and trying many things (and pulling my hair out..) I set it to a date in the future, my Birthday (2014, 5, 23) and other days. For those dates, Month was correctly set to 5 (May)
tl;dr
LocalDate.now()
.getYear()
java.time
The modern approach uses the industry-leading java.time classes.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still "yesterday" in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
Or, better, use the Month
enum objects pre-defined, one for each month of the year. Tip: Use these Month
objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety.
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
Parts
Interrogate for the parts as needed.
int dayOfMonth = ld.getDayOfMonth() ;
int month = ld.getMonthValue() ;
int year = ld.getYear() ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project 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.
这篇关于日历类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!