使用Java中Calendar的add()方法添加超过30天
我不太确定向 Java 日历对象添加超过 30 天时应使用哪个字段。 Calendar.DAY_OF_MONTH
和 Calendar.DAY_OF_YEAR
之间有什么区别吗?
示例:
GregorianCalendar d = new GregorianCalendar();
d.add(Calendar.DAY_OF_YEAR, 90);
vs
GregorianCalendar d = new GregorianCalendar();
d.add(Calendar.DAY_OF_MONTH, 90);
谢谢。
I'm not quite sure what field to use when adding more than 30 days to a Java Calendar object. Is there any difference in between Calendar.DAY_OF_MONTH
and Calendar.DAY_OF_YEAR
?
Example:
GregorianCalendar d = new GregorianCalendar();
d.add(Calendar.DAY_OF_YEAR, 90);
vs
GregorianCalendar d = new GregorianCalendar();
d.add(Calendar.DAY_OF_MONTH, 90);
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为当你调用 add 时这没有什么区别。当您调用吸气剂时,这种区别很重要。
两种方法都很好用,对吗?超过 30 天,以及负金额。
GregorianCalendar#add 的(诚然很复杂)源有这一部分:
I don't think it makes a difference when you call add. The distinction is important when you call the getters.
Both methods work fine, right? For more than 30 days, as well as negative amounts.
The (admittedly complicated) source for GregorianCalendar#add has this section:
tl;dr
详细信息
现在,使用现代 java.time 类取代了旧的
Calendar
& 变得更加容易。日期
类。LocalDate
LocalDate
类表示仅日期值,没有时间和时区。时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,在法国巴黎午夜过后几分钟,又是新的一天“昨天”在魁北克蒙特利尔。
以
大陆/地区
格式指定正确的时区名称 ,例如America/Montreal
、非洲/卡萨布兰卡
,或太平洋/奥克兰
。切勿使用 3-4 个字母的缩写,例如EST
或IST
,因为它们不是真正的时区,不是标准化的,甚至不是唯一的( !)。您可以添加天数。
Period
您可以使用
Period
类来表示一段时间。您可以通过调用
plus
或minus
方法来执行日期数学运算。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
Details
Much easier now with the modern java.time classes that supplant the old
Calendar
&Date
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.
Specify a proper time zone name in the format of
continent/region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. Never use the 3-4 letter abbreviation such asEST
orIST
as they are not true time zones, not standardized, and not even unique(!).You can add a number of days to that.
Period
You can represent a span of time with the
Period
class.You can perform date math by calling
plus
orminus
methods.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?
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.