如何将日期字符串转换为日期或日历对象?

时间:2021-09-17 16:49:24

I have a String representation of a date that I need to create a Date or Calendar object from. I've looked through Date and Calendar APIs but haven't found anything that can do this other than creating my own ugly parse method. I know there must be a way, does anyone know of a solution?

我有一个日期的字符串表示形式,我需要从中创建一个日期或日历对象。我查看了Date和Calendar api,但是除了创建我自己的难看的解析方法之外,还没有找到其他的方法。我知道一定有办法,有人知道解决办法吗?

5 个解决方案

#1


121  

In brief:

简而言之:

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
try {
  Date date = formatter.parse("01/29/02");
} catch (ParseException e) {
  e.printStackTrace();
}

See SimpleDateFormat javadoc for more.

更多信息请参见SimpleDateFormat javadoc。

And to turn it into a Calendar, do:

把它变成日历,要做的是:

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

#2


14  

tl;dr

LocalDate.parse( "2015-01-02" )

java.time

Java 8 and later has a new java.time framework that makes these other answers outmoded. This framework is inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. See the Tutorial.

Java 8和更高版本有一个新的Java。时间框架使其他答案过时。这个框架受到JSR 310定义的Joda-Time的启发,并由三个额外的项目扩展。看教程。

The old bundled classes, java.util.Date/.Calendar, are notoriously troublesome and confusing. Avoid them.

旧的绑定类,java.util.Date/。日历是出了名的麻烦和混乱。避免它们。

LocalDate

Like Joda-Time, java.time has a class LocalDate to represent a date-only value without time-of-day and without time zone.

像Joda-Time,java。time有一个类LocalDate来表示一个没有时间和没有时区的日期值。

ISO 8601

If your input string is in the standard ISO 8601 format of yyyy-MM-dd, you can ask that class to directly parse the string with no need to specify a formatter.

如果您的输入字符串是标准的ISO 8601格式的yyyy-MM-dd,您可以要求该类直接解析字符串,而不需要指定格式化程序。

The ISO 8601 formats are used by default in java.time, for both parsing and generating string representations of date-time values.

在java中默认使用ISO 8601格式。时间,用于解析和生成日期时间值的字符串表示。

LocalDate localDate = LocalDate.parse( "2015-01-02" );

Formatter

If you have a different format, specify a formatter from the java.time.format package. You can either specify your own formatting pattern or let java.time automatically localize as appropriate to a Locale specifying a human language for translation and cultural norms for deciding issues such as period versus comma.

如果您有不同的格式,请指定来自java.time的格式化程序。包格式。您可以指定自己的格式模式,也可以使用java。时间自动地本地化为适当的语言环境,指定用于翻译的人类语言,以及用于决定诸如句号和逗号等问题的文化规范。

Formatting pattern

Read the DateTimeFormatter class doc for details on the codes used in the format pattern. They vary a bit from the old outmoded java.text.SimpleDateFormat class patterns.

阅读DateTimeFormatter类doc,了解格式模式中使用的代码的详细信息。它们与过时的java.text稍有不同。SimpleDateFormat类模式。

Note how the second argument to the parse method is a method reference, syntax added to Java 8 and later.

注意,解析方法的第二个参数是方法引用、添加到Java 8以及以后的语法。

String input = "January 2, 2015";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "MMMM d, yyyy" , Locale.US );
LocalDate localDate = LocalDate.parse ( input , formatter );

Dump to console.

转储到控制台。

System.out.println ( "localDate: " + localDate );

localDate: 2015-01-02

localDate:2015-01-02

Localize automatically

Or rather than specify a formatting pattern, let java.time localize for you. Call DateTimeFormatter.ofLocalizedDate, and be sure to specify the desired/expected Locale rather than rely on the JVM’s current default which can change at any moment during runtime(!).

或者与其指定格式模式,不如使用java。时间定位。DateTimeFormatter打电话。ofLocalizedDate,并且一定要指定期望的/预期的语言环境,而不是依赖于JVM的当前默认值,它在运行时(!)可以随时更改。

String input = "January 2, 2015";
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG );
formatter = formatter.withLocale ( Locale.US );
LocalDate localDate = LocalDate.parse ( input , formatter );

Dump to console.

转储到控制台。

System.out.println ( "input: " + input + " | localDate: " + localDate );

input: January 2, 2015 | localDate: 2015-01-02

输入:2015年1月2日|

#3


9  

The highly regarded Joda Time library is also worth a look. This is basis for the new date and time api that is pencilled in for Java 7. The design is neat, intuitive, well documented and avoids a lot of the clumsiness of the original java.util.Date / java.util.Calendar classes.

备受推崇的Joda Time图书馆也值得一看。这是为Java 7编写的新日期和时间api的基础。该设计简洁、直观、文档化良好,避免了许多原始java.util的笨拙。日期/ java.util。日历类。

Joda's DateFormatter can parse a String to a Joda DateTime.

Joda的DateFormatter可以将字符串解析为Joda DateTime。

#4


0  

Try this:

试试这个:

DateFormat.parse(String)

#5


0  

The DateFormat class has a parse method.

DateFormat类有一个解析方法。

See http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html for more information.

更多信息请参见http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html。

#1


121  

In brief:

简而言之:

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
try {
  Date date = formatter.parse("01/29/02");
} catch (ParseException e) {
  e.printStackTrace();
}

See SimpleDateFormat javadoc for more.

更多信息请参见SimpleDateFormat javadoc。

And to turn it into a Calendar, do:

把它变成日历,要做的是:

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

#2


14  

tl;dr

LocalDate.parse( "2015-01-02" )

java.time

Java 8 and later has a new java.time framework that makes these other answers outmoded. This framework is inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. See the Tutorial.

Java 8和更高版本有一个新的Java。时间框架使其他答案过时。这个框架受到JSR 310定义的Joda-Time的启发,并由三个额外的项目扩展。看教程。

The old bundled classes, java.util.Date/.Calendar, are notoriously troublesome and confusing. Avoid them.

旧的绑定类,java.util.Date/。日历是出了名的麻烦和混乱。避免它们。

LocalDate

Like Joda-Time, java.time has a class LocalDate to represent a date-only value without time-of-day and without time zone.

像Joda-Time,java。time有一个类LocalDate来表示一个没有时间和没有时区的日期值。

ISO 8601

If your input string is in the standard ISO 8601 format of yyyy-MM-dd, you can ask that class to directly parse the string with no need to specify a formatter.

如果您的输入字符串是标准的ISO 8601格式的yyyy-MM-dd,您可以要求该类直接解析字符串,而不需要指定格式化程序。

The ISO 8601 formats are used by default in java.time, for both parsing and generating string representations of date-time values.

在java中默认使用ISO 8601格式。时间,用于解析和生成日期时间值的字符串表示。

LocalDate localDate = LocalDate.parse( "2015-01-02" );

Formatter

If you have a different format, specify a formatter from the java.time.format package. You can either specify your own formatting pattern or let java.time automatically localize as appropriate to a Locale specifying a human language for translation and cultural norms for deciding issues such as period versus comma.

如果您有不同的格式,请指定来自java.time的格式化程序。包格式。您可以指定自己的格式模式,也可以使用java。时间自动地本地化为适当的语言环境,指定用于翻译的人类语言,以及用于决定诸如句号和逗号等问题的文化规范。

Formatting pattern

Read the DateTimeFormatter class doc for details on the codes used in the format pattern. They vary a bit from the old outmoded java.text.SimpleDateFormat class patterns.

阅读DateTimeFormatter类doc,了解格式模式中使用的代码的详细信息。它们与过时的java.text稍有不同。SimpleDateFormat类模式。

Note how the second argument to the parse method is a method reference, syntax added to Java 8 and later.

注意,解析方法的第二个参数是方法引用、添加到Java 8以及以后的语法。

String input = "January 2, 2015";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "MMMM d, yyyy" , Locale.US );
LocalDate localDate = LocalDate.parse ( input , formatter );

Dump to console.

转储到控制台。

System.out.println ( "localDate: " + localDate );

localDate: 2015-01-02

localDate:2015-01-02

Localize automatically

Or rather than specify a formatting pattern, let java.time localize for you. Call DateTimeFormatter.ofLocalizedDate, and be sure to specify the desired/expected Locale rather than rely on the JVM’s current default which can change at any moment during runtime(!).

或者与其指定格式模式,不如使用java。时间定位。DateTimeFormatter打电话。ofLocalizedDate,并且一定要指定期望的/预期的语言环境,而不是依赖于JVM的当前默认值,它在运行时(!)可以随时更改。

String input = "January 2, 2015";
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG );
formatter = formatter.withLocale ( Locale.US );
LocalDate localDate = LocalDate.parse ( input , formatter );

Dump to console.

转储到控制台。

System.out.println ( "input: " + input + " | localDate: " + localDate );

input: January 2, 2015 | localDate: 2015-01-02

输入:2015年1月2日|

#3


9  

The highly regarded Joda Time library is also worth a look. This is basis for the new date and time api that is pencilled in for Java 7. The design is neat, intuitive, well documented and avoids a lot of the clumsiness of the original java.util.Date / java.util.Calendar classes.

备受推崇的Joda Time图书馆也值得一看。这是为Java 7编写的新日期和时间api的基础。该设计简洁、直观、文档化良好,避免了许多原始java.util的笨拙。日期/ java.util。日历类。

Joda's DateFormatter can parse a String to a Joda DateTime.

Joda的DateFormatter可以将字符串解析为Joda DateTime。

#4


0  

Try this:

试试这个:

DateFormat.parse(String)

#5


0  

The DateFormat class has a parse method.

DateFormat类有一个解析方法。

See http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html for more information.

更多信息请参见http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html。