1. Overview
1.概述
In this tutorial, we’ll explore several ways to convert String objects into Date objects. We’ll start with the new Date Time API, java.time, that was introduced in Java 8 before looking at the old java.util.Date data type also used for representing dates.
在本教程中,我们将探索将字符串对象转换为日期对象的几种方法。我们将从Java 8中引入的新的Date Time API,java.time开始,然后再看看同样用于表示日期的旧的java.util.Date数据类型。
To conclude, we’ll look at some external libraries for conversion using Joda-Time and the Apache Commons Lang DateUtils class.
最后,我们将看看一些使用Joda-Time和Apache Commons Lang DateUtils类进行转换的外部库。
2. Converting String to LocalDate or LocalDateTime
2.将String转换为LocalDate或LocalDateTime
LocalDate and LocalDateTime are immutable date-time objects that represent a date, and a date and time, respectively. By default, Java dates are in the ISO-8601 format, so if we have any string which represents a date and time in this format, then we can use the parse() API of these classes directly.
LocalDate 和LocalDateTime是不可变的日期-时间对象,分别代表一个日期,以及一个日期和时间。默认情况下,Java日期的格式是ISO-8601,所以如果我们有任何字符串代表这种格式的日期和时间,那么我们可以直接使用这些类的parse() API。
Interested in working as a Java developer intern? Check out Jooble!
有兴趣成为一名Java开发实习生?看看Jooble!
2.1. Using the Parse API
2.1.使用Parse API
The Date-Time API provides parse() methods for parsing a String that contains date and time information. To convert String objects to LocalDate and LocalDateTime objects, the String must represent a valid date or time according to ISO_LOCAL_DATE or ISO_LOCAL_DATE_TIME.
T日期-时间API提供了parse()方法来解析包含日期和时间信息的String。要将字符串对象转换为LocalDate和LocalDateTime对象,String必须根据ISO_LOCAL_DATE或ISO_LOCAL_DATE_TIME代表一个有效日期或时间。
Otherwise, a DateTimeParseException will be thrown at runtime.
否则,在运行时将抛出一个DateTimeParseException。
In our first example, let’s convert a String to a java.time.LocalDate:
在我们的第一个例子中,让我们将一个String转换为java.time.LocalDate。
LocalDate date = LocalDate.parse("2018-05-05");
A similar approach to the above can be used to convert a String to a java.time.LocalDateTime:
可以使用与上述类似的方法将String转换为java.time.LocalDateTime。
LocalDateTime dateTime = LocalDateTime.parse("2018-05-05T11:50:55");
It is important to note that both the LocalDate and LocalDateTime objects are timezone agnostic. However, when we need to deal with time zone specific dates and times, we can use the ZonedDateTime parse method directly to get a time zone specific date time:
需要注意的是,LocalDate和LocalDateTime对象都与时区无关。然而,当我们需要处理特定时区的日期和时间时,我们可以直接使用ZonedDateTime parse方法来获取特定时区的日期时间。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2015-05-05 10:15:30 Europe/Paris", formatter);
Now let’s have a look at how we convert strings with a custom format.
现在让我们来看看我们如何用自定义格式转换字符串。
2.2. Using the Parse API With a Custom Formatter
2.2.使用自定义格式化的Parse API
Converting a String with a custom date format into a Date object is a widespread operation in Java.
将具有自定义日期格式的String转换为Date对象是Java中的一个广泛的操作。
For this purpose we’ll use the DateTimeFormatter class, which provides numerous predefined formatters, and allows us to define a formatter.
为此,我们将使用DateTimeFormatter类,它提供了许多预定义的格式化,并允许我们定义一个格式化。
Let’s start with an example of using one of the predefined formatters of DateTimeFormatter:
让我们从使用DateTimeFormatter的一个预定义格式的例子开始。
String dateInString = "19590709";
LocalDate date = LocalDate.parse(dateInString, DateTimeFormatter.BASIC_ISO_DATE);
In the next example, let’s create a formatter that applies a format of “EEE, MMM d yyyy.” This format specifies three characters for the full day name of the week, one digit to represent the day of the month, three characters to represent the month, and four digits to represent the year.
在下一个例子中,让我们创建一个格式器,应用 “EEE, MMM d yyyy “的格式。这个格式为一周的全日名称指定了三个字符,一个数字代表月的一天,三个字符代表月,四个数字代表年。
This formatter recognizes strings such as “Fri, 3 Jan 2003″ or “Wed, 23 Mar 1994“:
该格式化器可以识别诸如”Fri, 3 Jan 2003″或 “Wed, 23 Mar 1994“的字符串。
String dateInString = "Mon, 05 May 1980";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy", Locale.ENGLISH);
LocalDate dateTime = LocalDate.parse(dateInString, formatter);
2.3. Common Date and Time Patterns
2.3.常见的日期和时间模式
Let’s look at some common date and time patterns:
让我们看一下一些常见的日期和时间模式。
- y – Year (1996; 96)
- M – Month in year (July; Jul; 07)
- d – Day in month (1-31)
- E – Day name in week (Friday, Sunday)
- a – AM/PM marker (AM, PM)
- H – Hour in day (0-23)
- h – Hour in AM/PM (1-12)
- m – Minute in hour (0-60)
- s – Second in minute (0-60)
For a full list of symbols that we can use to specify a pattern for parsing click here.
关于我们可以用来指定解析模式的符号的完整列表,请点击这里。
If we need to convert java.time dates into the older java.util.Date object, read this article for more details.
如果我们需要将java.time日期转换为较早的java.util.Date对象,请阅读这篇文章以了解更多细节。
3. Converting String to java.util.Date
3.将字符串转换为java.util.Date
Before Java 8, the Java date and time mechanism was provided by the old APIs of java.util.Date, java.util.Calendar, and java.util.TimeZone classes, which we sometimes still need to work with.
在Java 8之前,Java的日期和时间机制是由java.util.Date、java.util.Calendar和java.util.TimeZone类的旧API提供的,我们有时仍然需要使用它们。
Let’s see how to convert a String into a java.util.Date object:
让我们看看如何将一个字符串转换成java.util.Date对象。
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String dateInString = "7-Jun-2013";
Date date = formatter.parse(dateInString);
In the above example, we first need to construct a SimpleDateFormat object by passing the pattern describing the date and time format.
在上面的例子中,我们首先需要通过传递描述日期和时间格式的模式来构造一个SimpleDateFormat对象。
Next we need to invoke the parse() method passing the date String. If the String argument passed is not in the same format as the pattern, then a ParseException will be thrown.
接下来我们需要调用parse()方法,传递日期String。如果传递的String参数与模式的格式不一致,那么将抛出一个ParseException。
3.1. Adding Time Zone Information to java.util.Date
3.1.向java.util.Date添加时区信息
It’s important to note that the java.util.Date has no concept of time zone, and only represents the number of seconds passed since the Unix epoch time – 1970-01-01T00:00:00Z.
需要注意的是,java.util.Date没有时区的概念,它只表示自Unix纪元时间–1970-01-01T00:00:00Z以来的秒数。
However, when we print the Date object directly, it will always be printed with the Java default system time zone.
然而,当我们直接打印Date对象时,它将总是以Java默认的系统时区打印。
In this final example, we’ll look at how to format a date while adding the time zone information:
在这最后一个例子中,我们将看看如何在添加时区信息的同时格式化一个日期。
SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a", Locale.ENGLISH);
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String dateInString = "22-01-2015 10:15:55 AM";
Date date = formatter.parse(dateInString);
String formattedDateString = formatter.format(date);
We can also change the JVM time zone programmatically, but this isn’t recommended:
我们也可以通过编程来改变JVM的时区,但这并不推荐。
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
4. External Libraries
4.外部图书馆
Now that we have a good understanding of how to convert String objects to Date objects using the new and old APIs offered by core Java, let’s take a look at some external libraries.
现在我们已经很好地理解了如何使用核心Java提供的新旧API将String对象转换为Date对象,让我们看一下一些外部库。
4.1. Joda-Time Library
4.1.Joda-时间库
An alternative to the core Java Date and Time library is Joda-Time. Although the authors now recommend that users migrate to java.time (JSR-310), if this isn’t possible then the Joda-Time library provides an excellent alternative for working with Date and Time. This library provides pretty much all the capabilities supported in the Java 8 Date Time project.
核心Java Date和Time库的替代品是Joda-Time。尽管作者现在建议用户迁移到java.time(JSR-310),但如果这不可能,那么Joda-Time库为处理日期和时间提供了一个出色的替代方案。这个库提供了几乎所有在Java 8 Date Time项目中支持的功能。
The artifact can be found on Maven Central:
该工件可在Maven Central上找到。
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10</version>
</dependency>
Here’s a quick example working with the standard DateTime:
下面是一个使用标准DateTime的快速例子。
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
String dateInString = "07/06/2013 10:11:59";
DateTime dateTime = DateTime.parse(dateInString, formatter);
Let’s also see an example of explicitly setting a time zone:
我们也来看看一个明确设置时区的例子。
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
String dateInString = "07/06/2013 10:11:59";
DateTime dateTime = DateTime.parse(dateInString, formatter);
DateTime dateTimeWithZone = dateTime.withZone(DateTimeZone.forID("Asia/Kolkata"));
4.2. Apache Commons Lang – DateUtils
4.2 Apache Commons Lang – DateUtils
The DateUtils class provides many useful utilities, making it easier to work with the legacy Calendar and Date objects.
DateUtils类提供了许多有用的实用程序,使其更容易与传统的Calendar和Date对象一起工作。
The commons-lang3 artifact is available from Maven Central:
commons-lang3工件可从Maven Central获得。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Let’s convert a date String into a java.util.Date using an Array of date patterns:
让我们使用一个日期模式的Array将一个日期String转换为java.util.Date。
String dateInString = "07/06-2013";
Date date = DateUtils.parseDate(dateInString,
new String[] { "yyyy-MM-dd HH:mm:ss", "dd/MM-yyyy" });
5. Conclusion
5.总结
In this article, we illustrated several ways of converting Strings to different types of Date objects (with and without time), both in plain Java as well as using external libraries.
在这篇文章中,我们说明了几种将字符串转换为不同类型的Date对象(含时间和不含时间)的方法,既可以用普通的Java,也可以使用外部库。
The full source code for the article is available over on GitHub.
该文章的完整源代码可在GitHub上获得。