1

I am trying to print two dates using SimpleDateFormat but for my custom date the output looks completely different.

  DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date date2 =dateFormat.parse("01/01/2014 10:45:01");
    System.out.println(("date2:"+date2));
    Date date = new Date();
    System.out.println(dateFormat.format(date)); // how it prints this  is the desired outcome

OUTPUT:

date2:Wed Jan 01 10:45:01 GMT 2014
11/04/2014 10:45:50 
Athanatos
  • 1,089
  • 6
  • 15
  • 32
  • Date has no concept of a format, it is simply a container of the number of milliseconds since the unix epoch. It has a human readable output simply for convince. If you want to change how a date value looks, you need to pass it through a formatter and store the resulting String value for your own purposes – MadProgrammer Apr 11 '14 at 09:56
  • Output is ok. Since for date2 you not doing DateFormat .format(date2), you are just parsing it. But for date your formatting it to a given format. – Mukesh S Apr 11 '14 at 09:56
  • possible duplicate of [java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy](http://stackoverflow.com/questions/18480633/java-util-date-format-conversion-yyyy-mm-dd-to-mm-dd-yyyy) – MadProgrammer Apr 11 '14 at 09:57

5 Answers5

1

The output is correct. You are creating date2 by parsing the date in string with the DateFormat. But when you print date2, you are not printing it with dateFormat.format() and hence the date is printed in its default format.

Try System.out.println("date2:"+dateFormat.format(date2));

anirudh
  • 4,116
  • 2
  • 20
  • 35
0

format() will return you string of date in your desired format.

Date Object ---------->SDF Fomatter------>Formatted date in String

parse() accept the date in string format (your customize format) and return you Date Object

Formatted String date ------>SDF parse----->Date object

Wanna check, print it's value:

dateFormat.format(dateFormat.parse("01/01/2014 10:45:01"));
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
0

You have parsed it using dateformat, But you need to format it to get desired output.

System.out.println(("date2:"+dateFormat.format(date2)));

yogesh
  • 574
  • 6
  • 24
0

Did you try dateFormat.parse("dd/MM/yyyy HH:mm:ss"); ?

Andy_Lima
  • 129
  • 12
0

The line

System.out.println(("date2:"+date2));

Implicitly calls the toString() method on the date2 parameter. As Date has overridden the toString() method it inherits from Object, it is that method that dictates the format of the output. The Javadoc for Date#toString() states:

Converts this Date object to a String of the form: dow mon dd hh:mm:ss zzz yyyy

Which matches what you're seeing in your output. In order to get the output that you were expecting, you would need to do the following instead:

System.out.println(("date2:" + dateFormat.format(date2)));

Date objects don't have a format associated with them. They're a dumb object that doesn't need to know about any display formatting details, as they aren't relevant to the date itself.

JonK
  • 2,097
  • 2
  • 25
  • 36