vihang shah vihang shah - 1 year ago 99
Java Question

Exception in thread "main" java.time.format.DateTimeParseException: Text '14/Oct/2016 23:09:25.2110 +0530' could not be parsed at index 0

I was just playing with Java 8 Date time API. my code looks like this.

System.out.println("date now:"+date);
String ormat ="dd/MMM/yyyy HH:mm:ss"));
//=date.format(DateTimeFormatter.ofPattern("dd/MMM/yyyy'T'hh:mm:ss.SX Z"));
LocalDateTime date2=LocalDateTime.parse(format);

System.out.println("dsate now:"+date2);

But it is showing this error

Exception in thread "main" java.time.format.DateTimeParseException: Text '14/Oct/2016 23:13:57' could not be parsed at index 0

I tried with this pattern

String"dd/MMM/yyyy HH:mm:ss.SSSS Z"));

Still didn't work.

I checked the answers here and here.

One thing also I want to know what if I want my date object only in this format?

Edit 2:
What I try to achieve is that have some date and time what I got using localDateTime and I want to format it using the formatter I have used in code.

Answer Source

Your format2 produces the String 14/Oct/2016 23:26:38

You try to parse that input String with the ISO_LOCAL_DATE_TIME (yyyy-MM-dd'T'hh:mm:ss). That is why you were getting the error.

You have to pass the date formatter to the parse method

System.out.println("date now:"+date2);
String"dd/MMM/yyyy HH:mm:ss"));
//=date.format(DateTimeFormatter.ofPattern("dd/MMM/yyyy'T'hh:mm:ss.SX Z"));
LocalDateTime date3=LocalDateTime.parse(format2,
                DateTimeFormatter.ofPattern("dd/MMM/yyyy HH:mm:ss").withLocale(Locale.ENGLISH));

System.out.println("dsate now:"+date3);


dsate now:2016-10-14T23:26:38

Edit after the comment:

You can directly format the LocalDateTime

System.out.println("date now:"+date2);
String myDate1 = date2.format(DateTimeFormatter.ofPattern("dd/MMM/yyyy HH:mm:ss").withLocale(Locale.ENGLISH));
System.out.println("dsate now:"+myDate1);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download