I'm trying to format a Date object and convert that formatted one back to Date type object
This is my code
SimpleDateFormat inputDf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz");
System.out.println("before format "+invoiceDate);
invoiceDate=inputDf.parse(inputDf.format(invoiceDate));
System.out.println("after format "+inputDf.format(invoiceDate));
System.out.println("after parse "+invoiceDate);
Out put from above code is
before format : Mon Jan 14 10:55:40 IST 2013
after format : Mon Jan 14 2013 10:55:40 IST
after parse : Mon Jan 14 10:55:40 IST 2013
You can see here after i parse the date object it converting back to original format(format which shows before format) but i want Date object like it appear in second print line(after format) thing is .format method returns String not a Date object, how can i fix this ?
Thanks
This behaviour is because the SimpleDateFomat
does not impose itself upon the date. The SimpleDateFormat
is merely a pattern to get a formated output out of any Date
, but it does not change the date itself. Date
does not have a format, so
System.out.println("before format "+invoiceDate);
defaults to the default pattern format.
Actually, the SimpleDateFormat
is exactly the way to achieve what you want, ie use it to properly format your output everytime you need it. Formating a date object gives you a representation of it, a String
.