How to properly format the date?

What’s wrong in this code?

You seem to be expecting the returned Date object to know about the format you’ve parsed it from – it doesn’t. It’s just an instant in time. When you want a date in a particular format, you use SimpleDateFormat.format, it’s as simple as that. (Well, or you use a better library such as Joda Time.)

Think of the Date value as being like an int – an int is just a number; you don’t have “an int in hex” or “an int in decimal”… you make that decision when you want to format it. The same is true with Date.

(Likewise a Date isn’t associated with a specific calendar, time zone or locale. It’s just an instant in time.)

Leave a Comment