简体   繁体   English

Java中的日期算术,从今天起增加3个月

[英]Date arithmetic in Java, adding 3 months from today

My program is about generating (producing) a Kurosawa and making the customers produce it. 我的计划是关于生产(生产)黑泽明并让客户生产它。

Every time we generate a Kurosawa, we have to print its id, its production date and expiration date, which is 3 months from the production date. 每当我们生成黑泽明时,我们必须打印其生产日期的生产日期和生产日期,即生产日期和生效日期。

My problem is: How can I calculate the date after 3 months? 我的问题是: 如何计算3个月后的日期?

Use the built-in Java Calendar API . 使用内置的Java Calendar API

Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 3);

Refer to the API for exactly how to print out the date, in the format you are looking for. 请参阅API,了解如何以您要查找的格式打印日期。

You could also use the much more powerful and easier to use Joda Time Library : 您还可以使用更强大,更易于使用的Joda时间库

DateMidnight productionDate = new DateMidnight();
DateMidnight expirationDate = productionDate.plusMonths(3);
System.out.println(expirationDate.toString("dd.MM.yyyy"));

Joda Time has many advantages over the built-in Java Calendar API. 与内置的Java Calendar API相比,Joda Time具有许多优势

tl;dr TL;博士

LocalDate.now()            // Determine today’s date.
         .plusMonths( 3 )  // Add three months.

java.time java.time

The modern approach uses java.time classes. 现代方法使用java.time类。

The LocalDate class represents a date-only value without time-of-day and without time zone. LocalDate类表示没有时间且没有时区的仅日期值。

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. 如果未指定时区,则JVM会隐式应用其当前的默认时区。 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 . continent/region的格式指定适当的时区名称 ,例如America/MontrealAfrica/CasablancaPacific/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(!). 切勿使用3-4字母缩写,例如ESTIST因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

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

Add three months, letting java.time do the math. 添加三个月,让java.time做数学。

LocalDate threeMonthsLater = today.plusMonths( 3 ) ;

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

我相信Java Calendar Library应该可以帮到你。

如果您需要使用日期算术,JODA可以更好地工作,因为日历喜欢时间戳。

/ The java class Calendar is abstract. / java类Calendar是抽象的。 So you need to use the GregorianCalendar class. 所以你需要使用GregorianCalendar类。 / /

java.util.GregorianCalendar gC = new java.util.GregorianCalendar();

java.util.Date yourDate = new java.util.Date();
gC.setTime(yourDate);

/ Add 3 months / / 加3个月 /

gC.add(java.util.Calendar.MONTH_OF_YEAR, 3);

/ to go back 1 week / / 回去1周 /

gC.add(java.util.Calendar.WEEK_OF_YEAR, -1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM