I try to create a pattern to parse the date / time of Apache Access Log-a. Example

'30/Nov/2016:04:13:36 +0200' 

I set the pattern

 private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z"); 

As a result, I get an exception when using the parse method:

 Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '30/Nov/2016:04:13:36 +0200' could not be parsed at index 3 

How to set the pattern for DateTimeFormatter correctly? It seems to be doing everything according to the documentation, and still something is not right.

Edit: For template

 formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy:hh:mm:ss Z"); String datetime = "30/Nov/2016:04:13:36 +0200"; ZonedDateTime zonedDateTime = ZonedDateTime.parse(datetime, formatter); 

Gives exactly the same exception.

 Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '30/Nov/2016:04:13:36 +0200' could not be parsed at index 3 
  • Try to specify the locale (English or ROOT) when creating the formatter - zRrr
  • Yes, it worked. Thank. - Vanguard

2 answers 2

 import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Main{ private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z"); public static void main(String[] args){ String datetime = "30/Nov/2016:04:13:36 +0200"; ZonedDateTime zonedDateTime = ZonedDateTime.parse(datetime, formatter); System.out.println(zonedDateTime.toString()); } } 

http://ideone.com/ojxwIQ

Oddly enough, the original template is compiled and executed without errors on https://www.compilejava.net/ .

As it turned out, the reason is simple - you need to specify the locale, otherwise, for reasons unknown to me, it refuses to parse into Idea.

  private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z").withLocale(Locale.US); 

    Try this:

     DateTimeFormatter.ofPattern("dd/MMM/yyyy:hh:mm:ss Z") 

    Where:

    • dd day in two digits
    • MMM abbreviated month name
    • yyyy year
    • hh watch
    • mm minutes
    • ss seconds
    • Z time zone

    More information can be found in the documentation.

    For your case, ZonedDateTime is:

     String datetime = "30/Nov/2016:04:13:36 +0200"; TemporalAccessor time = DateTimeFormatter .ofPattern("dd/MMM/yyyy:HH:mm:ss Z") .parse(datetime); LocalDateTime localDateTime = LocalDateTime.of( time.get(YEAR), time.get(MONTH_OF_YEAR), time.get(DAY_OF_MONTH), time.get(HOUR_OF_DAY), time.get(MINUTE_OF_HOUR), time.get(SECOND_OF_MINUTE)); ZoneId zone = ZoneOffset.from(time); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zone); 
    • Same exception: Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '30 / Nov / 2016: 04: 13: 36 +0200 'could not be parsed at index 3 - Vanguard
    • Everything works for me, check better. - Artem Konovalov
    • Already found the reason, thanks for the effort :) - Vanguard