Reputation: 167
I have a TimeStamp '2013-06-24 10:46:11.0' and I need to cut off the .0 part, so what I did was to use the SimpleDateFormat to parse it to String and back then parse it to date, the first conversion was fine but the second (string to date) throws a java date time.
public void convert(Object object) {
Date date;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = object().getDate();
String formated = format.format(date);
try {
date = format.parse(formated);
} catch (ParseException ex) {
Logger.getLogger(DlgConsultaFactura.class.getName()).log(Level.SEVERE, null, ex);
}
}
What I expect is a date like this 2013-06-24 10:46:11, but what I got is this date Mon Jun 24 10:46:11 CDT 2013
Any help will be appreciated. Thanks.
Upvotes: 2
Views: 250
Reputation: 97
DateFormat i.e. SimpleDateFormat just formats the date as per your need.
Parse returns the Date representation of the String (or timestamp in your case) passed in. Format returns the String representation of the Date object passed in.
In both the cases , you see the same date just the representation is different.
Upvotes: 0
Reputation: 1576
Not the best way but a quick and easy one if you want just the string representation of the date ...
formated = formated.substring(0, formated.length()-2);
:)
Upvotes: 0
Reputation: 785058
Mon Jun 24 10:46:11 CDT 2013
and 2013-06-24 10:46:11
is actually same value. Mon Jun 24 10:46:11 CDT 2013
is as per your default locale.
You're getting confused between date's internal representation and its display format.
To print in 2013-06-24 10:46:11
you can use same SimpleDateFormat
object again.
You can use DateFormat#format(Date)
to print the date or return the String representation in your desired format i.e. "yyyy-MM-dd HH:mm:ss"
. Something like this:
String myDt = format.format(date);
// 2013-06-24 10:46:11
Upvotes: 2