Java 8 – How to parse date with "dd MMM" (02 Jan), without year?
2020-03-19 20:07
543 查看
https://mkyong.com/java8/java-8-how-to-parse-date-with-dd-mmm-02-jan-without-year/
This example shows you how to parse a date (02 Jan) without a year specified.
JavaDateExample.javapackage com.mkyong.time; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class JavaDateExample { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM", Locale.US); String date = "02 Jan"; LocalDate localDate = LocalDate.parse(date, formatter); System.out.println(localDate); System.out.println(formatter.format(localDate)); } }
Output
Exception in thread "main" java.time.format.DateTimeParseException: Text '02 Jan' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=2, MonthOfYear=1},ISO of type java.time.format.Parsed at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017) at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952) at java.base/java.time.LocalDate.parse(LocalDate.java:428) at com.mkyong.time.JavaDateExample.main(JavaDateExample.java:20) Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=2, MonthOfYear=1},ISO of type java.time.format.Parsed at java.base/java.time.LocalDate.from(LocalDate.java:396) at java.base/java.time.format.Parsed.query(Parsed.java:235) at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948) ... 2 more
Solution
The pattern dd MMM
is not enough; we need a DateTimeFormatterBuilder
to provide a default year for the date parsing.
package com.mkyong.time; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; import java.util.Locale; public class JavaDateExample { public static void main(String[] args) { DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("dd MMM") .parseDefaulting(ChronoField.YEAR, 2020) .toFormatter(Locale.US); String date = "02 Jan"; LocalDate localDate = LocalDate.parse(date, formatter); System.out.println(localDate); System.out.println(formatter.format(localDate)); } }
Output
2020-01-02 02 Jan
References
- Java 8 – How to convert String to LocalDate
- DateTimeFormatterBuilder JavaDoc
相关文章推荐
- java.text.ParseException: Unparseable date: "2016-11-02 00-02-00"
- Error when passing date to create solr date format.java.text.ParseException: Unparseable date: "We 4000 d
- java将 Date原始格式EEE MMM dd HH:mm:ss Z yyyy"转成指定格式
- How-to: resolve "Unapproved licenses:" issue during building with mvn+rat
- How to parse JSON in Java
- Unable to parse template "Annotation" Error message: This template did not produce a Java class or a
- How to write a UTF-8 file with Java? - Stack Overflow
- how to deal with "no such file error or diretory" error for a new programmer in QT creator
- java中设置日期格式的方式一般用SimpleDateFormat("YY-MM-dd")就可以了,但为什么有的 SimpleDateFormat("YY-MM-dd",locale.getDefau
- [译]How to Design Games Levels with "Super Mario World Method"
- Java: How to replace string "\" to "\\"
- How-to: Resolve "Datanode denied communication with namenode because hostname cannot be resolved (ip
- How-to: resolve "java.lang.NoClassDefFoundError: org/htrace/Trace" when hbase Export
- How to test java applet with Selenium
- How To Install Java on Ubuntu with Apt-Get
- java.net.UnknownHostException: Unable to resolve host "api102.meishi.cc": No address associated with hostname
- How To Install Java on Ubuntu with Apt-Get
- FAQ(59):java.text.ParseException: Unparseable date: "Wed Jan 24 16:28:14 CST 2018"
- How To Install Java on Ubuntu with Apt-Get
- JavaWeb: 严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]