使用Java中Calendar的add()方法添加超过30天

发布于 2024-08-26 04:39:14 字数 371 浏览 6 评论 0原文

我不太确定向 Java 日历对象添加超过 30 天时应使用哪个字段。 Calendar.DAY_OF_MONTHCalendar.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谷夏 2024-09-02 04:39:14

我认为当你调用 add 时这没有什么区别。当您调用吸气剂时,这种区别很重要。

两种方法都很好用,对吗?超过 30 天,以及负金额。

GregorianCalendar#add 的(诚然很复杂)源有这一部分:

 case DAY_OF_MONTH: // synonym of DATE
 case DAY_OF_YEAR:
 case DAY_OF_WEEK:
    break;

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:

 case DAY_OF_MONTH: // synonym of DATE
 case DAY_OF_YEAR:
 case DAY_OF_WEEK:
    break;
我们只是彼此的过ke 2024-09-02 04:39:14

tl;dr

LocalDate.now( ZoneId.of( "America/Montreal" ) )
         .plusDays( 30 )

详细信息

现在,使用现代 java.time 类取代了旧的 Calendar & 变得更加容易。 日期 类。

LocalDate

LocalDate 类表示仅日期值,没有时间和时区。

时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,在法国巴黎午夜过后几分钟,又是新的一天“昨天”在魁北克蒙特利尔

大陆/地区格式指定正确的时区名称 ,例如 America/Montreal非洲/卡萨布兰卡,或太平洋/奥克兰 。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的( !)。

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

您可以添加天数。

LocalDate later = today.plusDays( 30 );

Period

您可以使用Period 类来表示一段时间。

Period thirtyDays = Period.ofDays( 30 );

您可以通过调用 plusminus 方法来执行日期数学运算。

LocalDate later = today.plus( thirtyDays );

关于 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

LocalDate.now( ZoneId.of( "America/Montreal" ) )
         .plusDays( 30 )

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 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 );

You can add a number of days to that.

LocalDate later = today.plusDays( 30 );

Period

You can represent a span of time with the Period class.

Period thirtyDays = Period.ofDays( 30 );

You can perform date math by calling plus or minus methods.

LocalDate later = today.plus( thirtyDays );

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文