简体   繁体   English

Java:如何在不使用日历的情况下操作日期以添加天数或月数?

[英]Java: How can I manipulate date to add days or months without using Calendar?

I have a simple function that compares two dates, if the two dates are equal - I want to add 7 days to one of the dates, or add a month depending on the scenario.我有一个简单的 function 比较两个日期,如果两个日期相等 - 我想将其中一个日期添加 7 天,或者根据情况添加一个月。 I have two dates formatted using simpleDateFormat that are converted to date from a string, then I compare them like so我有两个使用 simpleDateFormat 格式化的日期,它们从字符串转换为日期,然后我像这样比较它们

Date dateExpired = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(expDate);
Date dateEffective = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(effDate);

Comparison method:比较方法:

if(dateExpired.before(dateEffective) || dateExpired.equals(dateEffective)) {
    System.out.println("Same date for effective and expired");
    
}

Inside my comparison, I want to make the update.在我的比较中,我想进行更新。 Can anyone tell me how to do this?谁能告诉我该怎么做?

Never use Calendar , part of the terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.永远不要使用Calendar ,这是几年前被 JSR 310 中定义的现代java.time类所取代的糟糕日期时间类的一部分。

Never put single-quote marks around the Z in your date-time formatting pattern.切勿在日期时间格式模式中的Z周围加上单引号。 That letter indicates an offset-from-UTC of zero hours-minutes-seconds.该字母表示与 UTC 的偏移量为零时分秒。 Your quotes are discarding valuable information.您的报价正在丢弃有价值的信息。

Your input strings are apparently in standard ISO 8601 format.您的输入字符串显然采用标准 ISO 8601 格式。 The java.time classes use the standard formats by default when when parsing/generating text. java.time类在解析/生成文本时默认使用标准格式。 So no need to specify a formatting pattern.所以不需要指定格式模式。

Parse your inputs as Instant objects to represent a moment as seen in UTC.将您的输入解析为Instant对象以表示在 UTC 中看到的时刻。

Instant expirationUtc = Instant.parse( x ) ;
Instant effectiveUtc = Instant.parse( y ) ;

For any given moment, the date varies around the globe by time zone.对于任何给定的时刻,日期在全球范围内因时区而异。 It can be “tomorrow” in Tokyo Japan while simultaneously “yesterday” in Toledo Ohio US.在日本东京可能是“明天”,而在美国俄亥俄州托莱多可能是“昨天”。

So you must adjust your inputs to the time zone by which you want to perceive the date.因此,您必须将输入调整为您想要感知日期的时区。

ZoneId z = ZoneId.of( "America/Edmonton" ) ; 
ZonedDateTime expiration = expirationUtc.atZone( z ) ;
ZonedDateTime effective = effectiveUtc.atZone( z ) ;

Test for equality of dates.测试日期是否相等。 Extract the date portion to compare.提取日期部分进行比较。

if( expiration.toLocalDate().isEqual( effective.toLocalDate() ) ) {…}

Represent a span of time using Period class, for your month or week.使用Period class 表示您的月或周的时间跨度。

Period period = Period.ofWeeks( 1 ) ;
ZonedDateTime later = expiration.plus( period ) ;

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

相关问题 如何在没有日历的情况下将月份添加到日期? - how to add months to the date without Calendar? 我想获取该月的最后一个日期并添加 n 个月,但日历实例在 Java 中只需要 30 天 - I want to get last date of the month and add n months to it but calendar instance is taking only 30 days in Java 如何在不使用日历的情况下使用日期添加天数 - how to add days using Date without using Calendar 如何从 Java 中的日历 object 构建天、月、年的列表? - How can I build a list of days, months, years from a calendar object in Java? 如何在不从Java API导入日期/日历的情况下向Java中的日期添加n天? - How to add n days to a Date in java without importing Date/Calendar from java API? Java - 我如何将日期作为输入并能够添加/减去其天/月/年 - Java - how do I take date as input and be able to add/subtract its days/months/years 我可以在不使用 Java 中的日历库的情况下将秒数添加到当前日期吗? - Can I add seconds to current date without using Calendar Library in Java? 如何在Java GWT中进行日历操作?如何在日期中添加天数? - How to do calendar operations in Java GWT? How to add days to a Date? 如何在Java中将当前日期添加到工作日? - How can I add business days to the current date in Java? 如何使用Java日历从日期中减去X天? - How to subtract X days from a date using Java calendar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM