简体   繁体   English

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

[英]How to convert a date String to a Date or Calendar object?

I have a String representation of a date that I need to create a Date or Calendar object from.我有一个日期的String表示形式,我需要从中创建一个DateCalendar对象。 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.我已经浏览了DateCalendar API,但除了创建我自己的丑陋解析方法之外,没有发现任何可以做到这一点的东西。 I know there must be a way, does anyone know of a solution?我知道一定有办法,有人知道解决方案吗?

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 = Calendar.getInstance();
calendar.setTime(date);

tl;dr tl;博士

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

java.time时间

Java 8 and later has a new java.time framework that makes these other answers outmoded. Java 8 及更高版本有一个新的java.time框架,使这些其他答案过时了。 This framework is inspired by Joda-Time , defined by JSR 310 , and extended by the ThreeTen-Extra project.该框架受Joda-Time 的启发,由JSR 310定义,并由ThreeTen-Extra项目扩展。 See the Tutorial .请参阅教程

The old bundled classes, java.util.Date/.Calendar, are notoriously troublesome and confusing.旧的捆绑类 java.util.Date/.Calendar 是出了名的麻烦和混乱。 Avoid them.避开它们。

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 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.如果您的输入字符串是yyyy-MM-dd的标准ISO 8601格式,您可以要求该类直接解析字符串,而无需指定格式化程序。

The ISO 8601 formats are used by default in java.time, for both parsing and generating string representations of date-time values. java.time 中默认使用 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.如果您有不同的格式,请从java.time.format包中指定一个格式化程序。 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 自动本地化到Locale指定用于翻译的人类语言和决定句点与逗号等问题的文化规范。

Formatting pattern格式化模式

Read the DateTimeFormatter class doc for details on the codes used in the format pattern.有关格式模式中使用的代码的详细信息,请阅读DateTimeFormatter类文档。 They vary a bit from the old outmoded java.text.SimpleDateFormat class patterns.它们与旧的过时的java.text.SimpleDateFormat类模式略有不同。

Note how the second argument to the parse method is a method reference , syntax added to Java 8 and later.请注意parse方法的第二个参数如何是方法引用,语法添加到 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本地日期:2015-01-02

Localize automatically自动本地化

Or rather than specify a formatting pattern, let java.time localize for you.或者不是指定格式模式,而是让 java.time 为您本地化。 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(!).调用DateTimeFormatter.ofLocalizedDate ,并确保指定所需/预期的Locale而不是依赖 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 |输入:2015 年 1 月 2 日 | localDate: 2015-01-02本地日期:2015-01-02

The highly regarded Joda Time library is also worth a look.备受推崇的Joda Time 库也值得一看。 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.这是为 Java 7编写的新日期和时间 API 的基础。设计简洁、直观、文档齐全,避免了原始java.util.Date / java.util.Calendar类的许多笨拙之处。

Joda's DateFormatter can parse a String to a Joda DateTime . Joda 的DateFormatter可以将 String 解析为 Joda DateTime

试试这个:

DateFormat.parse(String)

The DateFormat class has a parse method. DateFormat类有一个parse方法。

See DateFormat for more information.有关详细信息,请参阅日期格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM