Mercer
Mercer

Reputation: 9986

Joda Time IllegalArgumentException

I use Joda Time Framework. In My function i do this:

...
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(DateTime.now().toString());
...

i have this error:

java.lang.IllegalArgumentException: Invalid format: "2016-03-11T11:38:22.666+01:00" is malformed at "16-03-11T11:38:22.666+01:00"

What's wrong ..?

Update:

I use the @user2004685 code:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime newTime = formatter.parseDateTime(new Date().toString());
Date startDate =  newTime.toDate();
newTime = formatter.parseDateTime(DateTime.now().plusMonths(6).toString());
Date endDate = newTime.toDate();

i have this new error:

java.lang.IllegalArgumentException: Invalid format: "Fri Mar 11 12:09:36 CET 2016"

Upvotes: 0

Views: 809

Answers (2)

ninja.coder
ninja.coder

Reputation: 9648

Your string should be in same format if you want to parse it. Right now you are creating the String out of Date which will be in EEE MMM dd HH:mm:ss zzzz yyyy format but you are using a different format i.e. dd/MM/yyyy HH:mm:ss

Here is the code snippet:

DateTimeFormatter out = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zzzz yyyy");
DateTime newTime = out.parseDateTime(new Date().toString());

If you want to convert this Date into dd/MM/yyyy HH:mm:ss format then you can further do something like this:

DateTimeFormatter in = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
System.out.println(in.print(newTime));

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172428

You should try this:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

Upvotes: 1

Related Questions