0

I am using the below code to convert a date in String :

    String strDate="Thu Aug 09 16:01:46 IST 2018";        
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    LocalDateTime dateTime = LocalDateTime.parse(strDate,formatter);

I am getting the below exception :

java.time.format.DateTimeParseException: Text 'Thu Aug 09 16:01:46 IST 2018' could not be parsed at index 0

The format in the variable 'strDate' will be same and cannot be modified as i will be getting that from a different application

souvikc
  • 991
  • 1
  • 10
  • 25
  • I need to convert the input string to 'yyyy-MM-dd HH:mm:ss.SSS' format. – souvikc Aug 09 '18 at 16:57
  • The pattern of input string should match it with the parser, once you get the date from the parser, then again convert the date into your desired pattern. Its a two step process – Hemant Patel Aug 09 '18 at 17:12

1 Answers1

1

The date format for your input string would be : E MMM dd HH:mm:ss z yyyy. Below code should work without any error

public static void main(String[] args) throws IOException {
    String strDate = "Thu Aug 09 16:01:46 IST 2018";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy");
    LocalDateTime dateTime = LocalDateTime.parse(strDate, formatter);
}
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41