Converting a String to a Date in Groovy – 在Groovy中把一个字符串转换成一个日期

最后修改: 2019年 3月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll learn how to convert a String representing a date into a real Date object in Groovy.

在这个简短的教程中,我们将学习如何在Groovy中把代表日期的String转换为一个真正的Date对象。

However, we should keep in mind that this language is an enhancement of Java. Therefore, we can still use every plain old Java method, in addition to the new Groovy ones.

然而,我们应该记住,这种语言是对Java的一种增强。因此,除了新的Groovy方法外,我们仍然可以使用每一个普通的旧Java方法。

2. Using DateFormat

2.使用DateFormat

Firstly, we can parse Strings into Dates, as usual, using a Java DateFormat:

首先,我们可以像往常一样,使用Java DateFormat将字符串解析为日期。

def pattern = "yyyy-MM-dd"
def input = "2019-02-28"

def date = new SimpleDateFormat(pattern).parse(input)

Groovy, however, allows us to perform this operation more easily. It encapsulates the same behavior inside the convenience static method Date.parse(String format, String input):

然而,Groovy允许我们更容易地执行这一操作。它在方便的静态方法中封装了相同的行为 Date.parse(String format, String input)

def date = Date.parse(pattern, input)

In short, that method is an extension of the java.util.Date object, and internally it instantiates a java.text.DateFormat upon every invocation, for thread safety.

简而言之,该方法是java.util.Date对象的扩展,在内部它实例化了一个java.text.DateFormat 在每次调用时,为了线程安全,

2.1. Compatibility Issues

2.1.兼容性问题

To clarify, the Date.parse(String format, String input) method is available since version 1.5.7 of Groovy.

澄清一下,Date.parse(String format, String input)方法从Groovy的1.5.7版本开始可用。

Version 2.4.1 introduced a variant accepting a third parameter indicating a timezone: Date.parse(String format, String input, TimeZone zone).

2.4.1版本引入了一个接受第三个参数表示时区的变体。Date.parse(String format, String input, TimeZone zone)

From 2.5.0, however, there has been a breaking change and those enhancements are not shipped anymore with groovy-all.

然而,从2.5.0开始,出现了一个突破性的变化,这些增强功能不再与groovy-all一起交付。

So, going forward, they need to be included as a separate module, named groovy-dateutil:

所以,往后,它们需要作为一个单独的模块,命名为groovy-dateutil

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-dateutil</artifactId>
    <version>2.5.6</version>
</dependency>

There’s also version 3.0.0, but it’s currently in the Alpha stage.

还有3.0.0版本,但目前还在Alpha阶段。

3. Using JSR-310 LocalDate

3.使用JSR-310 LocalDate

Since version 8, Java introduced a whole new set of tools to handle with dates: the Date/Time API.

从第8版开始,Java引入了一套全新的工具来处理日期问题:Date/Time API

These APIs are better for several reasons and should be preferred over the legacy ones.

这些API由于一些原因而更好,应该比传统的API更受欢迎

Let’s see how to exploit the java.time.LocalDate parsing capabilities from Groovy:

让我们看看如何利用Groovy的java.time.LocalDate解析能力。

def date = LocalDate.parse(input, pattern)

4. Conclusion

4.结论

We’ve seen how to transform a String into a Date in the Groovy language, paying attention the peculiarities between the specific versions.

我们已经看到了如何在Groovy语言中把String转换为Date,注意到了具体版本之间的特殊性。

As always, the source code and unit tests are available over on GitHub.

一如既往,源代码和单元测试可在GitHub上获得。