Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JodaTime IllegalArgumentException: Invalid format

this Code is throwing the "invalid format" or "malformed" Exception:

Code:

strDate = "21/10/2015 12:00:00 AM";
format = "dd/MM/yyyy hh:mm:ss a";

DateTime.parse(strDate, DateTimeFormat.forPattern(format)).toDate();

Exception:

java.lang.IllegalArgumentException: Invalid format: "21/10/2015 12:00:00 AM"
is malformed at "AM"

What's wrong?

I'm ussing joda-time:2.8.1

(I already searched and i think this is the correct pattern for that strDate)

like image 635
Ninja Coding Avatar asked Jan 19 '26 16:01

Ninja Coding


2 Answers

The text of the AM/PM marker may be different to that of your default locale. You could do

Date date = 
  DateTime.parse(strDate, DateTimeFormat.forPattern(format).withLocale(Locale.US)).toDate();
like image 88
Reimeus Avatar answered Jan 21 '26 08:01

Reimeus


Depends on the locale, you may specify the locale and timezone :

these should work for you:

DateTime.parse(strDate, DateTimeFormat.forPattern(format).withLocale(Locale.US)).withZone(DateTimeZone.UTC).toDate();

or

DateTime.parse(strDate, DateTimeFormat.forPattern(format).withLocale(Locale.US)).toDate();
like image 36
nil Avatar answered Jan 21 '26 08:01

nil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!