Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime exception while trying to parse date time using joda DateTimeFormatter

Tags:

java

jodatime

I am executing following code which gives me

"Exception in thread "main" org.joda.time.IllegalFieldValueException: Cannot parse "12/17/2017 23:10": Value 23 for clockhourOfHalfday must be in the range [1,12]"

DateTimeFormatter dt = DateTimeFormat.forPattern("MM/dd/yyyy hh:mm");
System.out.println(dt.parseDateTime("12/17/2017 23:10"));

If I will parse "12/17/2017 02:10" then It executes successfully.

So basically, I need to parse time having 24hr clock format.

Thanks in advance.

like image 881
Rahul Avatar asked Jan 17 '14 14:01

Rahul


2 Answers

h is for halfday in JodaTime as you can see in the exception.
You need to use H (or possibly k):

DateTimeFormatter dt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm");
System.out.println(dt.parseDateTime("12/17/2017 23:10"));

There is more information in the DateTimeFormat API.

like image 153
Keppil Avatar answered Sep 21 '22 03:09

Keppil


It can be achieved with Simple Date Format too.

DateFormat readDate = new SimpleDateFormat( "MM/dd/yyyy HH:mm");
Date date = readDate.parse("12/17/2017 23:10");
DateFormat writeDate = new SimpleDateFormat( "MM/dd/yyyy HH:mm");
System.out.println(writeDate.format(date));
like image 27
Vinayak Pingale Avatar answered Sep 22 '22 03:09

Vinayak Pingale