I am trying to parse a date format string and then print out the formatted string for the same pattern. I am expecting them to be the same. But the output is not agreeing with the input. Any help is much appreciated
Here is my code
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test{
public static void main(String[] args) {
String pattern=args[0];
String dtstr = args[1];
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = new Date();
try {
//
String fmtd = sdf.format(date);
System.out.println("The formatted string: " +fmtd);
System.out.println("The parsed date is: " + sdf.format(sdf.parse(fmtd)));
System.out.println("The parsed date from input is: " + sdf.format(sdf.parse(dtstr)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here is how I invoke this
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/ sudo java Test "EEE MMM dd YYYY HH:mm:ss Z (zzzz)" "Sun Sep 22 2013 20:03:46 +0530 (India Standard Time)"
Here is the output
The formatted string: Wed Sep 25 2013 19:55:57 +0000 (Coordinated Universal Time)
The parsed date is: Wed Jan 02 2013 19:55:57 +0000 (Coordinated Universal Time)
The parsed date from input is: Sun Dec 30 2013 20:03:46 +0530 (India Standard Time)
I am expecting the formatted string
and the parsed strings
to be identical
What I am doing wrong?