2

Does anyone know how to parse a date such as: Mon Aug 04 16:07:00 CEST 2014 to dd/MM/YYYY HH:MM:SS using DateTime formatter from Joda. I've tried that:

final DateTimeFormatter sdf = DateTimeFormat.forPattern(DATE_FORMAT);
DateTime lastDateOnline = sdf.parseDateTime(lastCommunicationToDisplay.getDateOnLine().toString());
return lastDateOnline.toString();

DATE_FORMAT = dd/MM/YYYY HH:MM:SS and lastCommunicationToDisplay.getDateOnLine().toString() = Mon Aug 04 16:07:00 CEST 2014

I can't find clear explanations about that library. I'm requested to use that instead of SimpleDateFormat because it's not threadsafe.

2
  • 1
    You will want to parse it with the format it has, then format it with your new format. Aug 4, 2014 at 16:05
  • 1
    Why would the format dd/MM/YYYY HH:MM:SS parse Mon Aug 04 16:07:00 CEST 2014? Do you really see a match between the format and the string? Have you read the javadoc?
    – JB Nizet
    Aug 4, 2014 at 16:06

1 Answer 1

8

Solutions

If all you have to do is convert a LocalDate to a string respecting the pattern: "dd/MM/YYYY HH:mm:ss", then you can do it in a simpler way, using the overloaded toString() methods on LocalDate:

a) the one which receives the format string directly:

LocalDate date = lastCommunicationToDisplay.getDateOnLine();
System.out.println(date.toString("dd/MM/YYYY HH:mm:ss"));

b) the one which receives a DateTimeFormatter initialized with the aforementioned string:

DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm:ss");
LocalDate date = lastCommunicationToDisplay.getDateOnLine();
System.out.println(date.toString(dtf));

What went wrong in your code

The format string you are using is not compatible with the date string you are sending as input. The way you used DateTimeFormatter is used for parsing strings that are in that format to LocalDates, not the other way around.

The format would be appropriate if your input string would look like the following: 04/08/2014 22:44:33

Since yours looks differently, the following value of the format is compatible (provided your timezone is always CEST):

DATE_FORMAT = "E MMM dd HH:mm:ss 'CEST' YYYY";

So the entire code should look like this:

String dateString = "Mon Aug 04 16:07:00 CEST 2014";
DateTimeFormatter dtf = DateTimeFormat.forPattern("E MMM dd HH:mm:ss 'CEST' YYYY");
LocalDate date = dtf.parseLocalDate(dateString);
System.out.println(date.toString("MM/dd/yyyy")); // or use toString(DateTimeFormatter) and use your pattern with a small adjusment here (dd/MM/YYYY HH:mm:ss)

However, I recommend one of the first 2 suggestions.

1
  • Thank you so much for that answer. It's really complete. Aug 4, 2014 at 21:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.