Java DateTimeFormatter: DateTimeParseException with GMT post

I would like to Wed, 21 Oct 2016 07:28:00 GMT date, like Wed, 21 Oct 2016 07:28:00 GMT with DateTimeFormatter , in Instant . To create the template, I used the official documentation: DateTimeFormatter

My code is:

 String date = "Wed, 21 Oct 2016 07:28:00 GMT"; DateTimeFormatter gmtFormat = DateTimeFormatter.ofPattern("EEE' dd LLL yyyy HH:mm:ss '''"); TemporalAccessor parsed = gmtFormat.parse(date); Instant a = Instant.from(parsed); System.out.println(a); 

But every time I get this error:

 Exception in thread "main" java.time.format.DateTimeParseException: Text 'Wed, 21 Oct 2016 07:28:00 GMT' could not be parsed at index 0 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1777) 

At index 0 there is Wed , I used EEE , which represents day-of-week by definition, there are also examples that represent: Tue; Tuesday; T Tue; Tuesday; T Tue; Tuesday; T

I also tried using bottom and top, but not successfully. What's wrong? Did I miss something?

+5
source share
3 answers

It works with code

  String date = "Fri, 21 Oct 2016 07:28:00 GMT"; DateTimeFormatter gmtFormat = DateTimeFormatter.RFC_1123_DATE_TIME; TemporalAccessor parsed = gmtFormat.parse(date); Instant a = Instant.from(parsed); System.out.println(a); 

But you have to change the day, because otherwise it does not fit the given date.

+5
source

EEE is the correct format for Wed. But it does not include a comma.

Try the following:

 DateTimeFormatter gmtFormat = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss '''"); 

Useful link

+1
source

You request redundant information: day of the week and date.

Change the format to just ignore the first three characters, causing it to read day / month / year.

0
source

Source: https://habr.com/ru/post/1276192/


All Articles