在java中如何将一个字符串转换为日期?如何进行日期格式化?
在特定情况下,“ 2010年1月2日”作为输入字符串:
MMMM
d
yyyy
String string = "January 2, 2010"; DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH); Date date = format.parse(string); System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010
注意显式Locale参数的重要性。如果您省略它,那么它将使用默认语言环境,该语言环境不一定是输入字符串的月份名称中使用的英语。如果语言环境与输入字符串不匹配,那么java.text.ParseException即使格式模式似乎有效,您也可能会混淆。
以下是javadoc的相关摘录,列出了所有可用的格式模式:
Letter Date or Time Component Presentation Examples ------ ---------------------- ------------------ ------------------------------------- G Era designator Text AD y Year Year 1996; 96 Y Week year Year 2009; 09 M/L Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue u Day number of week Number 1 a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-08:00 Z Time zone RFC 822 time zone -0800 X Time zone ISO 8601 time zone -08; -0800; -08:00
请注意,这些模式区分大小写,并且四个或更多字符的基于文本的模式代表完整形式;否则,请使用简短形式或缩写形式。因此,例如MMMMM不需要更多。
MMMMM
以下是一些有效SimpleDateFormat模式的示例,这些模式可以解析迄今为止的给定字符串:
SimpleDateFormat
Input string Pattern ------------------------------------ ---------------------------- 2001.07.04 AD at 12:08:56 PDT yyyy.MM.dd G 'at' HH:mm:ss z Wed, Jul 4, '01 EEE, MMM d, ''yy 12:08 PM h:mm a 12 o'clock PM, Pacific Daylight Time hh 'o''clock' a, zzzz 0:08 PM, PDT K:mm a, z 02001.July.04 AD 12:08 PM yyyyy.MMMM.dd GGG hh:mm aaa Wed, 4 Jul 2001 12:08:56 -0700 EEE, d MMM yyyy HH:mm:ss Z 010704120856-0700 yyMMddHHmmssZ 2001-07-04T12:08:56.235-0700 yyyy-MM-dd'T'HH:mm:ss.SSSZ 2001-07-04T12:08:56.235-07:00 yyyy-MM-dd'T'HH:mm:ss.SSSXXX 2001-W27-3 YYYY-'W'ww-u
一个重要的注意的是,SimpleDateFormat是不是线程安全的。换句话说,您永远不应声明并将其分配为静态或实例变量,然后从不同的方法/线程重复使用它。您应该始终在方法本地范围内全新创建它。
如果您恰巧是使用Java 8或更高版本的Java DateTimeFormatter,请使用(也在此处,单击链接以查看所有预定义的格式器和可用的格式模式;该教程可在此处获得)。这个新的API受到JodaTime的启发。
String string = "January 2, 2010"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH); LocalDate date = LocalDate.parse(string, formatter); System.out.println(date); // 2010-01-02
注意:如果您的格式模式也恰好包含时间部分,请使用[LocalDateTime#parse(text, formatter)](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-)代替[LocalDate#parse(text, formatter)](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-)。并且,如果您的格式模式也恰好包含时区,请[ZonedDateTime#parse(text, formatter)](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-)改用。
[LocalDateTime#parse(text, formatter)](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-)
[LocalDate#parse(text, formatter)](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-)
[ZonedDateTime#parse(text, formatter)](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-)
Symbol Meaning Presentation Examples ------ -------------------------- ------------ ---------------------------------------------- G era text AD; Anno Domini; A u year year 2004; 04 y year-of-era year 2004; 04 D day-of-year number 189 M/L month-of-year number/text 7; 07; Jul; July; J d day-of-month number 10 Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter Y week-based-year year 1996; 96 w week-of-week-based-year number 27 W week-of-month number 4 E day-of-week text Tue; Tuesday; T e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T F week-of-month number 3 a am-pm-of-day text PM h clock-hour-of-am-pm (1-12) number 12 K hour-of-am-pm (0-11) number 0 k clock-hour-of-am-pm (1-24) number 0 H hour-of-day (0-23) number 0 m minute-of-hour number 30 s second-of-minute number 55 S fraction-of-second fraction 978 A milli-of-day number 1234 n nano-of-second number 987654321 N nano-of-day number 1234000000 V time-zone ID zone-id America/Los_Angeles; Z; -08:30 z time-zone name zone-name Pacific Standard Time; PST O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00; X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15; x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15; Z zone-offset offset-Z +0000; -0800; -08:00;
请注意,它具有一些用于更流行模式的预定义格式器。因此DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);,可以代替使用DateTimeFormatter.RFC_1123_DATE_TIME。这是可能的,因为与SimpleDateFormat线程安全相反,它们是线程安全的。因此,如果需要,您也可以定义自己的名称。
DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH)
DateTimeFormatter.RFC_1123_DATE_TIME
与SimpleDateFormat
对于特定的输入字符串格式,您不需要使用显式的DateTimeFormatter:标准ISO 8601日期(例如2016-09-26T17:44:57Z)可以直接解析,LocalDateTime#parse(text)因为它已经使用了ISO_LOCAL_DATE_TIME格式化程序。同样,[LocalDate#parse(text)](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-)解析不包含时间成分的ISO日期(请参阅[ISO_LOCAL_DATE](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE)),并[ZonedDateTime#parse(text)](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#parse-java.lang.CharSequence-)解析添加了偏移量和时区的ISO日期(请参阅[ISO_ZONED_DATE_TIME](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_ZONED_DATE_TIME))。
DateTimeFormatter
LocalDateTime#parse(text)
[LocalDate#parse(text)](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-)
[ISO_LOCAL_DATE](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE)
[ZonedDateTime#parse(text)](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#parse-java.lang.CharSequence-)
[ISO_ZONED_DATE_TIME](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_ZONED_DATE_TIME)