Java 如何将日期字符串转换为日期或日历对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a date String to a Date or Calendar object?
提问by Aaron
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?
我有一个String
日期的表示,我需要从中创建一个Date
或Calendar
对象。我已经通过看Date
和Calendar
API,但没有发现任何可以比创建我自己的丑陋的解析方法做其他。我知道一定有办法,有人知道解决方案吗?
采纳答案by Matt Sheppard
In brief:
简单来说:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
try {
Date date = formatter.parse("01/29/02");
} catch (ParseException e) {
e.printStackTrace();
}
See SimpleDateFormat
javadocfor more.
有关更多信息,请参阅SimpleDateFormat
javadoc。
And to turn it into a Calendar
, do:
并将其变成 a Calendar
,请执行以下操作:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
回答by Apocalisp
Try this:
尝试这个:
DateFormat.parse(String)
回答by Alexander Stolz
The DateFormat
class has a parse
method.
这个DateFormat
类有一个parse
方法。
See http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.htmlfor more information.
有关详细信息,请参阅http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html。
回答by serg10
The highly regarded Joda Time libraryis also worth a look. This is basis for the new date and time apithat is pencilled in for Java 7. The design is neat, intuitive, well documentedand 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
.
JodaDateFormatter
可以将 String 解析为 Joda DateTime
。
回答by Basil Bourque
tl;dr
tl;博士
LocalDate.parse( "2015-01-02" )
java.time
时间
Java 8 and later has a new java.timeframework that makes these other answers outmoded. This framework is inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extraproject. See the Tutorial.
Java 8 及更高版本有一个新的java.time框架,使这些其他答案过时了。该框架受Joda-Time 的启发,由JSR 310定义,并由ThreeTen-Extra项目扩展。请参阅教程。
The old bundled classes, java.util.Date/.Calendar, are notoriously troublesome and confusing. Avoid them.
旧的捆绑类 java.util.Date/.Calendar 是出了名的麻烦和混乱。避开它们。
LocalDate
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 8601format 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.time 中默认使用 ISO 8601 格式,用于解析和生成日期时间值的字符串表示。
LocalDate localDate = LocalDate.parse( "2015-01-02" );
Formatter
格式化程序
If you have a different format, specify a formatter from the java.time.formatpackage. 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.format包中指定一个格式化程序。您可以指定自己的格式模式,也可以让 java.time 根据需要自动本地化以Locale
指定用于翻译的人类语言和用于决定句点与逗号等问题的文化规范。
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
有关格式模式中使用的代码的详细信息,请阅读类文档。它们与旧的过时的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. 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.time 为您本地化。调用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 | localDate: 2015-01-02
输入:2015 年 1 月 2 日 | 本地日期:2015-01-02