Thanks for your response.
I am using following code
String date = "2006-01-17 15:19:57.0";
SimpleDateFormat sdfInput =
new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
try {
Date d = sdfInput.parse(date);
System.out.println("(Default) Today is " + d);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
After running this I am getting following exception
java.text.ParseException: Unparseable date: "2006-01-17 15:19:57.0"
I will appriciate your help
Thanks
You're specifying a 'T' and a timezone in your format, but they're not present in the string you're parsing.
I'm assuming from the way you're printing out the date, that your thinking is along these lines: "sdfInput will parse the input string, no matter what format it's in, and will produce a Date object. That Date object wil have the format specified in sdfInput."
This is wrong on a couple of fronts:
1) DateFormat doesn't magically figure out what format it's supposed to use for the String it's parse()***. The String has to match the DF's format.
2) Dates don't have formats. Only Strings do. A Date object is just a long. There's no relationship whatsoever between the Date that you get from parse() and the format that was used to produce it. When you print out a Date as you're doing, its toString method is called, which in turn uses a default format for your Locale.
If you want to turn a date string in one format into a date string in another format, use two different DateFormat objects with two different formats. Date date = df1.parse(inputString);
String outputString = df2.format(date);