1. Overview
1.概述
In this tutorial, we’ll look at ways to increment date by one day using Java. Before Java 8, the standard Java date and time libraries weren’t very user-friendly. Hence, Joda-Time became the de facto standard date and time library for Java prior to Java 8.
在本教程中,我们将看看用Java将日期递增一天的方法。在Java 8之前,标准的Java日期和时间库并不十分友好。因此,在Java 8之前,Joda-Time成为事实上的Java标准日期和时间库。
There are also other classes and libraries that could be used to accomplish the task, like java.util.Calendar and Apache Commons.
还有其他的类和库可以用来完成任务,比如java.util.Calendar和Apache Commons。
Java 8 included a better date and time API to address the shortcomings of its older libraries.
Java 8包括一个更好的日期和时间API,以解决其旧库的缺点。
Therefore, we’re looking at how to increment date by one day using Java 8, Joda-Time API, Java’s Calendar class and Apache Commons library.
因此,我们要研究的是如何使用Java 8、Joda-Time API、Java的日历类和Apache Commons库将日期递增一天。
2. Maven Dependencies
2.Maven的依赖性
The following dependencies should be included in the pom.xml file:
以下依赖关系应包括在pom.xml文件中。
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
You can find the latest version of the Joda-Time on Maven Central, and also the latest version of Apache Commons Lang.
您可以在Maven Central上找到最新版本的Joda-Time,也可以在Apache Commons Lang上找到最新版本。
3. Using java.time
3.使用java.time
The java.time.LocalDate class is an immutable date-time representation, often viewed as year-month-day.
java.time.LocalDate类是一个不可变的日期时间表示,通常被视为年-月-日。
LocalDate has many methods for date manipulation, let’s see how we can use it to accomplish the same task:
LocalDate有许多日期操作的方法,让我们看看如何使用它来完成同样的任务。
public static String addOneDay(String date) {
return LocalDate
.parse(date)
.plusDays(1)
.toString();
}
In this example, we’re using java.time.LocalDate class and its plusDays() method to increment the date by one day.
在这个例子中,我们使用java.time.LocalDate类和它的plusDays()方法来将日期增加一天。
Now, let’s verify that this method is working as expected:
现在,让我们验证一下这个方法是否如预期的那样工作。
@Test
public void givenDate_whenUsingJava8_thenAddOneDay()
throws Exception {
String incrementedDate = addOneDay("2018-07-03");
assertEquals("2018-07-04", incrementedDate);
}
4. Using java.util.Calendar
4.使用java.util.Calendar
Another approach is using java.util.Calendar and its add() method to increment the date.
另一种方法是使用java.util.Calendar和它的add()方法来增加日期。
We’ll use it along with java.text.SimpleDateFormat for date formatting purposes:
我们将把它和java.text.SimpleDateFormat一起用于日期格式化的目的。
public static String addOneDayCalendar(String date)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, 1);
return sdf.format(c.getTime());
}
java.text.SimpleDateFormat is there to ensure the expected date format is used. The date is increased via the add() method.
java.text.SimpleDateFormat是为了确保使用预期的日期格式。日期是通过add() 方法增加的。
Once again, let’s make sure this approach works as intended:
再一次,让我们确保这种方法能按预期的那样运作。
@Test
public void givenDate_whenUsingCalendar_thenAddOneDay()
throws Exception {
String incrementedDate = addOneDayCalendar("2018-07-03");
assertEquals("2018-07-04", incrementedDate);
}
5. Using Joda-Time
5.使用Joda-Time
The org.joda.time.DateTime class has many methods that help to properly deal with date and time.
org.joda.time.DateTime类有许多方法可以帮助正确处理日期和时间。
Let’s see how we can use it to increment the date by one day:
让我们看看如何使用它来将日期增加一天。
public static String addOneDayJodaTime(String date) {
DateTime dateTime = new DateTime(date);
return dateTime
.plusDays(1)
.toString("yyyy-MM-dd");
}
Here, we use org.joda.time.DateTime class and its plusDays() method to increment the date by one day.
在这里,我们使用org.joda.time.DateTime类和它的plusDays()方法来将日期增加一天。
We can verify that the code above works with the following unit test:
我们可以通过下面的单元测试来验证上面的代码是否工作。
@Test
public void givenDate_whenUsingJodaTime_thenAddOneDay() throws Exception {
String incrementedDate = addOneDayJodaTime("2018-07-03");
assertEquals("2018-07-04", incrementedDate);
}
6. Using Apache Commons
6.使用Apache Commons
Another library commonly used for date manipulation (among other things) is Apache Commons. It’s a suite of utilities surrounding the use of the java.util.Calendar and java.util.Date objects.
另一个常用于日期操作的库(除其他外)是Apache Commons。它是一套围绕使用java.util.Calendar和java.util.Date对象的实用工具。
For our task, we can use the org.apache.commons.lang3.time.DateUtils class and its addDays() method (note that SimpleDateFormat is again used for date formatting):
对于我们的任务,我们可以使用org.apache.commons.lang3.time.DateUtils类及其addDays()方法(注意,SimpleDateFormat再次用于日期格式化)。
public static String addOneDayApacheCommons(String date)
throws ParseException {
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
Date incrementedDate = DateUtils
.addDays(sdf.parse(date), 1);
return sdf.format(incrementedDate);
}
As usual, we’ll verify the results with a unit test:
像往常一样,我们将用单元测试来验证结果。
@Test
public void givenDate_whenUsingApacheCommons_thenAddOneDay()
throws Exception {
String incrementedDate = addOneDayApacheCommons(
"2018-07-03");
assertEquals("2018-07-04", incrementedDate);
}
7. Conclusion
7.结论
In this quick article, we looked at various approaches to dealing with a simple task of incrementing date by one day. We’ve shown how it can be accomplished using Java’s core APIs as well as some popular 3rd party libraries.
在这篇简短的文章中,我们研究了各种方法来处理将日期递增一天的简单任务。我们展示了如何使用Java的核心API以及一些流行的第三方库来完成这个任务。
The code samples used in this article can be found over on GitHub.
本文中使用的代码样本可以在GitHub上找到over。