1. Introduction
1.导言
Joda-Time is a very popular Java library concerning date and time manipulation. It gives a much more intuitive and flexible API than that usually offered by the standard DateTime class.
Joda-Time 是一个非常流行的有关日期和时间操作的 Java 库。它提供了比标准 DateTime 类更直观、更灵活的 API。
In this tutorial, we’ll look at how to transform Joda-Time DateTime objects into standard Java Date ones and vice versa.
在本教程中,我们将了解如何将 Joda-Time DateTime 对象转换为标准 Java Date 对象,反之亦然。
2. Setting Up Joda-Time
2.设置 Joda-Time
First, we should ensure that our project includes the joda-time library:
首先,我们应确保项目包含 joda-time 库:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.6</version>
</dependency>
Alternatively, we can download the jar file and put it in our classpath.
或者,我们可以 下载 jar 文件,并将其放入 classpath 中。
3. Convert Joda-Time DateTime to Java Date
3.将 Joda-Time DateTime 转换为 Java Date</em
To convert a Joda-Time DateTime object to a standard Java Date we use the method called toDate(). Below is a simple example:
要将 Joda-Time DateTime 对象转换为标准 Java Date 对象,我们可以使用名为 toDate() 的方法。下面是一个简单的示例:
@Test
public void givenJodaDateTime_whenConvertingToJavaDate_thenConversionIsCorrect() {
DateTime jodaDateTime = new DateTime();
java.util.Date javaDate = jodaDateTime.toDate();
assertEquals(jodaDateTime.getMillis(), javaDate.getTime());
}
In this test method, we create a new instance of DateTime from Joda-Time named jodaDateTime. Subsequently, we called the toDate() method on this Joda DateTime instance to obtain a corresponding java.util.Date object.
在此测试方法中,我们从 Joda-Time 中创建了一个名为 jodaDateTime 的 DateTime 新实例。随后,我们调用了该 Joda DateTime 实例上的 toDate() 方法,以获得相应的 java.util.Date 对象。
The test is performed using the assertEquals method, which asserts that the time in milliseconds retrieved from the original Joda DateTime object equals the time obtained from the new DateTime object created through the java.util.Date.
测试使用 assertEquals 方法进行,该方法断言从原始 Joda DateTime 对象获取的以毫秒为单位的时间等于从通过 java.util.Date 创建的新 DateTime 对象获取的时间。
4. Convert Java Date to Joda-Time DateTime
4.将 Java Date 转换为 Joda-Time DateTime</em
It is also straightforward to convert a plain Java Date object into Joda-Time DateTime. We can use the DateTime constructor designed for java.util.Date parameter as follows:
将普通 Java Date 对象转换为 Joda-Time DateTime 也很简单。我们可以使用为 java.util.Date 参数设计的 DateTime 构造函数,如下所示:
@Test
public void givenJavaDate_whenConvertingToJodaDateTime_thenConversionIsCorrect() {
java.util.Date javaDate = new java.util.Date();
DateTime jodaDateTime = new DateTime(javaDate);
assertEquals(javaDate.getTime(), jodaDateTime.getMillis());
}
Within the above test method, we actively instantiate a new java.util.Date object, representing the current date and time. Subsequently, we create a corresponding Joda DateTime object using the provided Java Date. The actual validation occurs using the assertEquals method, where we verify that the time in milliseconds is retrieved from the original java.util.Date object is equal to the time represented by the Joda DateTime object
在上述测试方法中,我们主动实例化了一个新的 java.util.Date 对象,该对象代表当前日期和时间。随后,我们使用提供的 Java Date 创建一个相应的 Joda DateTime 对象。实际验证是通过 assertEquals 方法进行的,我们在该方法中验证了从原始 java.util.Date 对象中获取的以毫秒为单位的时间是否等于 Joda DateTime 对象所代表的时间。
5. Conclusion
5.结论
In conclusion, one of the usual operations when working with dates and time in Java is converted between Joda-Time DateTime objects and standard Java Date.
总之,在 Java 中处理日期和时间时,通常的操作之一是在 Joda-Time DateTime 对象和标准 Java Date 之间进行转换。
Now that we have gone through the examples presented above, it should be easy for us to implement Joda-Time in our projects and easily convert these two types.
通过上面的示例,我们应该很容易在项目中实现 Joda-Time,并轻松转换这两种类型。
As usual, the accompanying source code can be found over on GitHub.
与往常一样,您可以在 GitHub 上找到随附的源代码。