Java Datetimeformatterbuilder Fails on Testtime

Unable to parse some dates into ZonedDateTime

        String incomingDate1 = "Fri, 11 Jun 2021 02:25:23 +0000";
String incomingDate2 = "Sun, 30 May 2021 11:42:03 +0000";

ZonedDateTime parsed = ZonedDateTime.parse(incomingDate1,
DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH));

System.out.println(parsed);

Cannot parse E MMM dd yyyy date

Specify Locale

Specify a locale, to determine a human language and culture to use in translation.

final DateTimeFormatterBuilder dateTimeFormatterBuilder = new DateTimeFormatterBuilder()
.appendOptional( DateTimeFormatter.ofPattern( "dd-MM-yyyy" ) )
.appendOptional( DateTimeFormatter.ofPattern( "dd/MM/yyyy" ) )
.appendOptional( DateTimeFormatter.ofPattern( "E MMM dd yyyy" ) )
.parseDefaulting( ChronoField.HOUR_OF_DAY, 0 )
.parseDefaulting( ChronoField.MINUTE_OF_HOUR, 0 )
.parseDefaulting( ChronoField.SECOND_OF_MINUTE, 0 );

String input = "Tue Apr 19 2022" ;
Locale locale = Locale.US ;
final LocalDateTime ldt = LocalDateTime.parse( input , dateTimeFormatterBuilder.toFormatter().withLocale( locale ) );
System.out.println ( ldt ) ;

See this code run live at IdeOne.com.

2022-04-19T00:00

Java.time - Parsing a month's name not working

The date String will be parsed with the default Locale (Locale.GERMAN maybe ?).

If the text is not in the according language, you have to specify the right Locale, e.g :

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d yyyy").withLocale(Locale.ENGLISH);

SimpleDateFormat causing unparseable error

You should use RFC_1123_DATE_TIME formatter.

public static void main(String[] args) {
String source = "Thu, 19 Dec 2019 11:32:04 +0000";
DateTimeFormatter mFormatter = DateTimeFormatter.RFC_1123_DATE_TIME;

System.out.println(OffsetDateTime.parse(source, mFormatter));
}

By the way, your pattern was good, you should just have added .withLocale().

DateTimeFormatter mFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss Z").withLocale(Locale.US);

Java YearMonth will not parse, but only on my computer?

Specify a Locale

Likely that the default locale of your JVM is not one that recognizes “Feb” as month of February.

Specify a Locale to determine the human language and cultural norms to be used in parsing the name of month.

Locale locale = Locale.US ; 
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM-yy" , locale ) ;
YearMonth yearMonth = YearMonth.parse( "Feb-17" , f );

See this code run live at IdeOne.com.

ISO 8601

Using localized text for data-exchange is unwise. I suggest you educate the publisher of your data about the ISO 8601 standard formats for exchanging date-time values as text.

The standard format for year-month is YYYY-MM. Example: 2017-02.

The java.time classes use ISO 8601 formats by default.

YearMonth.parse( "2017-02" )

java.time.format.DateTimeParseException: Text could not be parsed at index 0

Instead of "Wed", we need to use short weekdays 'So, Mo, Di, Mi, Do, Fr, Sa' in German.

String date = convertDateTimeString( "EEE, dd MMM yyyy HH:mm:ss z", "yyyy", "Mi, 04 Feb 
2015 10:12:34 UTC");

// output 2015

java.time.format.DateTimeParseException: Text 'Tue Jan 08 00:00:00 IST 2019' could not be parsed at index 0

This is probably due to the locale setting on your computer.
You might provide a Locale when creating the DateTimeFormatter.

DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.US);

This will ensure that the date is always parsed right.



Related Topics



Leave a reply



Submit