由 DateTimeFormatter
處理的解析是從字符串創(chuàng)建日期時間對象。
用于格式化的相同符號用作解析字符串值。
如果無法解析文本,則會拋出 DateTimeParseException
。它有兩種方法來提供錯誤的詳細信息。 getErrorIndex()
返回文本中的錯誤偏移量。 getParsedString()
返回正在解析的文本。
日期時間相關(guān)的類和DateTimeFormatter定義了將字符串解析為datetime對象的方法。
每個datetime類有兩個重載版本的 parse()
。 parse()
方法的返回類型與定義datetime類相同。
下面的代碼顯示了如何使用 parse()
方法從 LocalDate
對象:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { LocalDate ld1 = LocalDate.parse("2014-06-10"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate ld2 = LocalDate.parse("06/10/2014", formatter); System.out.println("ld1: " + ld1); System.out.println("ld2: " + ld2); } }
上面的代碼生成以下結(jié)果。
DateTimeFormatter
包含幾個將字符串解析為datetime對象的parse()方法。
它們中的大多數(shù)返回一個TemporalAccessor對象,您可以查詢它以獲取datetime組件。
您可以將TemporalAccessor對象傳遞給datetime類的from()方法,以獲取特定的datetime對象。
以下代碼顯示如何使用DateTimeFormatter對象解析MM/dd /yyyy格式的字符串,以構(gòu)造LocalDate:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAccessor; public class Main { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); TemporalAccessor ta = formatter.parse("06/10/2014"); LocalDate ld = LocalDate.from(ta); System.out.println(ld); } }
上面的代碼生成以下結(jié)果。
parse()方法可以通過TemporalQuery將字符串直接解析為特定的datetime對象。
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate ld = formatter.parse("06/10/2014", LocalDate::from); System.out.println(ld); } }
上面的代碼生成以下結(jié)果。
DateTimeFormatter類包含一個parseBest()方法。
parseBest()方法嘗試匹配與提供的格式的字符串可選格式符號。
在以下模式中,我們有兩個可選模式。
yyyy-MM-dd["T"HH:mm:ss[Z]]
文本可以完全解析為OffsetDateTime,并部分解析為LocalDateTime和LocalDate。
以下代碼顯示如何使用可選模式從字符串獲取最佳匹配日期時間對象。
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.TemporalAccessor; public class Main { public static void main(String[] args) { DateTimeFormatter parser = DateTimeFormatter .ofPattern("yyyy-MM- dd["T"HH:mm:ss[Z]]"); parseStr(parser, "2014-06-31"); parseStr(parser, "2014-06-31T15:31:12"); parseStr(parser, "2014-06-31T15:31:12-0500"); parseStr(parser, "2014-06-31Hello"); } public static void parseStr(DateTimeFormatter formatter, String text) { try { TemporalAccessor ta = formatter.parseBest(text, OffsetDateTime::from, LocalDateTime::from, LocalDate::from); if (ta instanceof OffsetDateTime) { OffsetDateTime odt = OffsetDateTime.from(ta); System.out.println("OffsetDateTime: " + odt); } else if (ta instanceof LocalDateTime) { LocalDateTime ldt = LocalDateTime.from(ta); System.out.println("LocalDateTime: " + ldt); } else if (ta instanceof LocalDate) { LocalDate ld = LocalDate.from(ta); System.out.println("LocalDate: " + ld); } else { System.out.println("Parsing returned: " + ta); } } catch (DateTimeParseException e) { System.out.println(e.getMessage()); } } }
上面的代碼生成以下結(jié)果。
更多建議: