将 java.util.Date 增加一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3809119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:21:57  来源:igfitidea点击:

Incrementing a java.util.Date by one day

javadate

提问by Anthony

What is the correct way to increment a java.util.Date by one day.

将 java.util.Date 增加一天的正确方法是什么。

I'm thinking something like

我在想像

        Calendar cal = Calendar.getInstance();
        cal.setTime(toDate);
        cal.add(Calendar.DATE, 1);
        toDate = cal.getTime();

It doesn't 'feel' right.

它“感觉”不对。

回答by Thilo

That would work.

那行得通。

It doesn't 'feel' right.

它“感觉”不对。

If it is the verbosity that bothers you, welcome to the Java date-time API :-)

如果是冗长的内容困扰您,欢迎使用 Java 日期时间 API :-)

回答by Matthew Flaschen

Yeah, that's right. Java Date APIs feel wrong quite often. I recommend you try Joda Time. It would be something like:

是啊,没错。Java Date API 经常感觉不对。我建议您尝试Joda Time。它会是这样的:

DateTime startDate = ...
DateTime endDate = startDate.plusDays(1);

or:

或者:

Instant start = ...
Instant end = start.plus(Days.days(1).toStandardDuration());

回答by Alfred

I believe joda timelibrary makes it much more clean to work with dates.

我相信joda 时间库使处理日期变得更加干净。

回答by Tony Ennis

Here's how I do it:

这是我的方法:

Date someDate = new Date(); // Or whatever    
Date dayAfter = new Date(someDate.getTime()+(24*60*60*1000));

Where the math at the end converts a day's worth of seconds to milliseconds.

最后的数学将一天的秒数转换为毫秒。

回答by Ivin

If you do not like the math in the solution from Tony Ennis

如果您不喜欢 Tony Ennis 的解决方案中的数学

Date someDate = new Date(); // Or whatever
Date dayAfter = new Date(someDate.getTime() + TimeUnit.DAYS.toMillis( 1 ));

But more or less since finding this Q/A, I have been using JodaTime, instead, and have recently switched to the new DateTime in Java 8 (which inspiredby but not copied from Joda - thanks @BasilBourqueless for pointing this out).

但或多或少自从找到这个 Q/A 后,我一直在使用JodaTime,并且最近切换到 Java 8 中的新 DateTime (它的灵感来自但不是从 Joda 复制的 - 感谢 @BasilBourqueless 指出这一点)。

Java 8

爪哇 8

In Java 8, almost all time-based classes have a .plusDays()method making this task trivial:

在 Java 8 中,几乎所有基于时间的类都有一个.plusDays()方法,使这个任务变得微不足道:

LocalDateTime.now()  .plusDays(1);
LocalDate.now()      .plusDays(1);
ZonedDateTime.now()  .plusDays(1);
Duration.ofDays(1)   .plusDays(1);
Period.ofYears(1)    .plusDays(1);
OffsetTime.now()     .plus(1, ChronoUnit.DAYS);
OffsetDateTime.now() .plus(1, ChronoUnit.DAYS);
Instant.now()        .plus(1, ChronoUnit.DAYS);

Java 8 also added classes and methods to interoperate between the (now) legacy Date and Calendar etc. and the new DateTime classes, which are most certainly the better choice for all new development.

Java 8 还添加了类和方法来在(现在)遗留的 Date 和 Calendar 等与新的 DateTime 类之间进行互操作,这无疑是所有新开发的更好选择。

回答by Catalin

If Java 8 or Joda Time are not an option, you could always opt for Apache DateUtils:

如果 Java 8 或 Joda Time 不是一个选项,你总是可以选择 Apache DateUtils:

DateUtils.addDays(myDate, 1);

回答by ZURA Tikaradze

Date thisDate = new Date(System.currentTimeMillis());

Date dayAfter = new Date(thisDate.getTime() + TimeUnit.DAYS.toMillis( 1 ));

Date dayBefore = new Date(thisDate.getTime() + TimeUnit.DAYS.toMillis( -1 ));

回答by Ole V.V.

I am contributing the modern answer.

我正在贡献现代答案。

It doesn't 'feel' right.

它“感觉”不对。

My suggestion is that why it doesn't feel right is because it's so verbose. Adding one day to a date is conceptually a simple thing to do and ought to have a simple representation in your program. The problem here is the poor design of the Calendarclass: when you use it, your code needs to be this verbose. And you are correct: you should notuse the poorly designed Calendarclass. It's long outdated. Using java.time, the modern Java date and time API, adding one day is simple, also in code:

我的建议是,为什么感觉不对是因为它太冗长了。将一天添加到日期在概念上是一件简单的事情,并且应该在您的程序中具有简单的表示形式。这里的问题是类的糟糕设计Calendar:当你使用它时,你的代码需要如此冗长。你是正确的:你应该使用设计不当Calendar班。它已经过时了。使用 java.time,现代 Java 日期和时间 API,添加一天很简单,也在代码中:

    LocalDate toDate = LocalDate.of(2020, Month.FEBRUARY, 28);
    LocalDate nextDate = toDate.plusDays(1);
    System.out.println("Incremented date is " + nextDate);

Output is:

输出是:

Incremented date is 2020-02-29

增量日期为 2020-02-29

In case your need is for a different type than LocalDate, other java.time classes also have a plusDaysmethod that you can use in the same way, as already shown in the answer by Ivin.

如果您需要与 不同的类型LocalDate,其他 java.time 类也有一个plusDays您可以以相同方式使用的方法,如 Ivin 的回答中所示。

Links

链接