Take the 2-minute tour ×

I'm having troubles getting the correct format for a DateTime. I'm expecting format dd/MM/yyyy HH:mm:ss and I'm getting too much verbose instead. For instance:

        DateTime dt;
        String timeStamp = day + "/" + month + "/" + year + " " + hour + ":" + minute + ":00"; 
        DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
        dt = formatter.parseDateTime(timeStamp);

And I'm getting things like:


Thus, why is the formatter not formatting as expected? Any clues?

Thanks! Alex

share|improve this question
Um, because you have dt = new DateTime() and have therefore not saved any use of the formatter? Also, what are you using to output the DateTime to get this result? Show that code. –  John B Jun 13 '13 at 13:22
The last line is a copy&paste error, I'm obviously not recreating the object there again :) –  AlejandroVK Jun 14 '13 at 8:42
add comment

4 Answers

up vote 0 down vote accepted

You told the formatter how to read your hand-crafted timeStamp String. You did not tell your system how to output the resulting DateTime object.

As Daniel Arndt pointed out below, you do that like this:


Understand that a DateTime object has no format. The format is what turns a string into a DateTime object, and a DateTime object into a string. What you see on your screen are always strings. So if you do an output of your DateTime object, as above, you also need to tell the sytem how to format it. If you do not, it will use a system specific default format, which is what happened when you got the 2013-06-13T00:15:00.000+02:00 string.

Long story short: Your DateTime object contains the correct data. And if you provide the proper format, it will look exactly like you expect, when you output it.

share|improve this answer
Aha, any idea how to do that? –  AlejandroVK Jun 14 '13 at 8:46
@AlejandroVK I just edited my answer with more information. Hope that helps. –  Jan Doerrenhaus Jun 14 '13 at 11:05
That was my understanding, just wanted to be sure, thanks, correct answer! –  AlejandroVK Jun 14 '13 at 11:06
add comment

You are only using the formatter for parsing, not formatting. If you used it for formatting as well e.g. with the print() method you would see the string you expect.

Also you appear to be parsing the string and immediately replacing it with the current date time. I would remove you last line or all the lines before it.

share|improve this answer
Thank, but...I don't want a String at the end, I want to keep the DateTime object, since its a member of a data class, thus, if I inspect the resulting DateTime object...but I guess I'll have to change it to String instead and discard using DateTime –  AlejandroVK Jun 14 '13 at 8:45
@AlejandroVK A DateTime is a date and a time, it doesn't have a format. When you try to print it just uses the default format for that type. Only a String has a format but it is just a bunch of characters. You shouldn't confuse a bunch of characters with a value which represents a time, or number as they are not the same thing. –  Peter Lawrey Jun 14 '13 at 11:51
That is correct, thanks –  AlejandroVK Jun 14 '13 at 11:54
add comment

The line

dt = new DateTime();

replaces all your efforts put by you before.So please remove it and print the dt

share|improve this answer
Thanks for the response, but that was a copy & paste error, my mistake –  AlejandroVK Jun 14 '13 at 8:41
Nope, not fixed, proposed solutions only depict returning a String, which I don't want, a need a DateTime with dd/MM/yyyy HH:mm:ss format... –  AlejandroVK Jun 14 '13 at 9:19
add comment

You need to print the date with the formatter. The following code should work if you preform it before the dt = new DateTime(); line:

share|improve this answer
add comment

Your Answer


