问题
I'm being passed a date in ths format - "JAN01/2020" but I can't seem to find the DateTimeFormatter pattern for it. I tried these (as well as several others) but they're resulting in DateTimeParseException -
DateTimeFormatter.ofPattern("MMMd/YYYY").parse("JAN01/2020")
DateTimeFormatter.ofPattern("MMMdd/YYYY").parse("JAN01/2020")
I also considered this post's solution and the three lines below also result in DateTimeParseException. It does not appear to be a case-sensitivity issue Java 8 DateTimeFormatter for month in all CAPS not working
DateTimeFormatter formatter= DateTimeFormatter.ofPattern("MMMdd/yyyy");
LocalDateTime dateTime = LocalDateTime.parse("JAN14/2020", formatter);
System.out.println(dateTime.getYear());
I appreciate any suggestions!
回答1:
You may
handle the case with use of
DateTimeFormatterBuilder
.parseCaseInsensitive()
for the case of the month,MMM
is forJan
and you haveJAN
.appendPattern("MMMdd/yyyy")
for the pattern
parse into a
LocalDate
as there is no time composant
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("MMMdd/yyyy");
DateTimeFormatter dateFormat = builder.toFormatter();
LocalDate dateTime = LocalDate.parse("JAN14/2020", dateFormat);
System.out.println(dateTime); // 2020-01-14
CODE DEMO
回答2:
Case-sensitive
The main problem with your format is, you are using YYYY
instead of yyyy
. Do it as follows:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
class Main {
public static void main(String args[]) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("MMMd/yyyy")
.toFormatter();
System.out.println(LocalDate.parse("JAN01/2020", formatter));
}
}
Output:
2020-01-01
Demo of using DateTimeFormatter::parse
with it:
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
class Main {
public static void main(String args[]) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("MMMd/yyyy")
.toFormatter();
System.out.println(formatter.parse("JAN01/2020"));
}
}
Output:
{},ISO resolved to 2020-01-01
回答3:
- As the question you are linking to says, you need to use a
DateTimeFormatterBuilder
for case insensitive parsing of your month abbreviation in upper case. - Beware of the case of format pattern letters.
yyyy
andYYYY
are different. See for example this question: Can't parse String to LocalDate (Java 8). Supply a locale for your formatter. If
JAN
is English, then supply an English-speaking locale. For example:new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern("MMMdd/yyyy") .toFormatter(Locale.ENGLISH)
As azro said, since you haven’t got a time of day, parse into a
LocalDate
, not aLocalDateTime
(or specify a default hour of day on the builder to be able to parse into aLocalDateTime
, but I don’t see the advantage).
来源:https://stackoverflow.com/questions/62074063/what-pattern-of-datetimeformat-is-needed-for-datetimeformatter-to-parse-jan01-20