Java:减去日期的最简单方法 [英] Java: Easiest Way to Subtract Dates

查看:109
本文介绍了Java:减去日期的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含两个必须为日期的字段的类,开始日期 date_passed 。我一直在研究java中具有 YYYY MM DD 格式的日期的最佳方法,该格式可以轻松地减去日期,并可以​​补足日期,例如说将来。

I have created a class with two fields that need to be dates, start_date and date_passed. I have been researching the best way in java to have dates in a YYYY MM DD format that allows for easy date subtraction, and the ability to "make-up" a date, say in the future for example.

我想做的事示例...

Example of what I'd like it to do...

library.circulate_book("Chemistry", **start date here**) //actual date or random date
library.pass_book("Chemistry", **Date Passed here**) //random date such as 5 days after start date
int days_had = Date_Passed - start_date



<到目前为止,我已经找到了很多使用Calendars和Date类格式化日期的方法,但是考虑到大多数日期都以字符串结尾,因此还没有找到一种看起来可行的方法。任何建议/小例子,不胜感激!此外,所有示例链接都很棒!

So far, I've found plenty of ways to format dates using Calendars and Date classes, but have yet to find one that looks like it would work considering most dates end up as Strings. Any suggestions/small examples are greatly appreciated! Also, any links to examples would be awesome!

推荐答案

tl; dr



通过加/减几天从一个日期移动到另一个日期。

tl;dr

To move from one date to another by adding/subtracting a number of days.

LocalDate.now(
    ZoneId.of( "Pacific/Auckland" ) 
)
.minusDays( 5 )

计算两个日期之间经过的天数,月数和年数。

To calculate the number of days, months, and years elapsed between two dates.

Period.between( start , stop )



解析



首先,您必须将字符串输入解析为日期时间对象。然后,您将使用这些对象来构成业务逻辑

不要将日期时间值视为字符串,这会让您发疯。我们在代码中使用日期时间对象;我们使用该日期时间对象的String表示形式与用户或其他应用交换数据。

Stop thinking of date-time values as strings, that will drive you nuts. We work with date-time objects in our code; we exchange data with users or other apps using a String representation of that date-time object.

在Java 8和更高版本中,使用 java.time 框架。请参见教程

In Java 8 and later, use the java.time framework. See Tutorial.

您只需要一个日期,没有时间,因此我们可以使用 LocalDate 类。

You want only a date, without time-of-day, so we can use the LocalDate class.

这种时髦的双冒号语法是方法参考

That funky double-colon syntax is a method reference, a way to say what method should be called by other code.

String input = "2015 01 02";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyy MM dd" );
LocalDate localDate = formatter.parse ( input , LocalDate :: from );



当前日期



确定今天的日期需要时区。在任何给定时刻,日期都会在全球范围内变化。

Current date

Determining today’s date requires a time zone. For any given moment, the date varies around the globe by zone.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate todayTunis = LocalDate.now( z ) ;

如果需要JVM的当前默认时区,请调用 ZoneId.systemDefault

If you want the JVM’s current default time zone, call ZoneId.systemDefault.

在StackOveflow.com上,该问题已被解决过很多次。 。例如,如何使用Java日历从日期中减去X天?。有关详细信息,请参见其他答案,例如我本人我自己的一本以获取更多详细信息。提示:经过是关键词。

This has been addressed many times before on StackOveflow.com. For example, How to subtract X days from a date using Java calendar?. For details, see other Answers such as this one by me and this one by me for more details. Tip: "elapsed" is a key search word.

简而言之,请使用 Period 来定义许多标准的日,月和年。

Briefly, use a Period to define a number of standard days, months, and years.

LocalDate weekLater = localDate.plusDays ( 7 );
Period period = Period.between ( localDate , weekLater );
Integer daysElapsed = period.getDays ();

转储到控制台。

System.out.println ( "localDate: " + localDate + " to " + weekLater + " in days: " + daysElapsed );




localDate:2015年1月2日至2015年1月9日天:7

localDate: 2015-01-02 to 2015-01-09 in days: 7

请注意,期间仅适用于整天,也就是说,没有日期的仅日期值。这就是我们在此问题中需要的,因此工作已完成。如果您有日期时间值( ZonedDateTime 对象),请使用 ThreeTen-Extra 项目的 Days 类,如上面链接的问题/答案中所述。

Note the limitation that Period works only with whole days, that is, date-only values without time-of-day. That is what we need here for this Question, so job done. If you had date-time values (ZonedDateTime objects), use the ThreeTen-Extra project’s Days class as noted in linked Question/Answer above.

这篇关于Java:减去日期的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆