I am extracting a Date object from a String using this piece of code:
Date.from(ZonedDateTime.parse(exchange.getIn().getHeader("Date").toString(), DateTimeFormatter.RFC_1123_DATE_TIME).toInstant());
But I have errors when the String is something like that: Tue, 26 Nov 2019 09:00:01 +0100 (CET)
How can I parse this kind of string obtaining a Date object? Do I need to use another DateTimeFormatter?
Yes, you can use a custom
DateTimeFormatterto parse the string in the format "Tue, 26 Nov 2019 09:00:01 +0100 (CET)". The format you provided contains additional information in parentheses, which is not part of the standard RFC 1123 format.Here's an example of how you might have some code to handle that case using a custom
DateTimeFormatter:This defined a custom
DateTimeFormattermatching the format "EEE, d MMM yyyy HH:mm:ss Z (zzz)".Following that, parse
dateStringusing the formatter, returning aZonedDateTimeobject. Grab theInstantfromZonedDateTimeand convert it back toDate. This should successfully parse the string you provided and do so via aDateobject.Sample Java Class
Given the string: "Tue, 26 Nov 2019 09:00:01 +0100 (CET)" This program prints: Tue Nov 26 03:00:01 EST 2019.
Perhaps I did not need to specify parsing because my installation is already set to an English speaking locale, but you can try the Locale.ROOT parameter at the end of the parser.