developer
developer

Reputation: 9488

date is saving in different format when saving to database

Iam saving date in below format EEE MMM dd HH:mm:ss z yyyy, but it is saving in this format 2013-05-03 00:38:20.0. But when i print same date on console before saving to database, it is as expected INFO: Fri May 03 00:38:20 IST 2013

Please can anyone explain why it is saving as above mentioned.

Below is the code:

Date now = new Date();
String datetimeStr = now.toString();
SimpleDateFormat format = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss z yyyy");
Date parseDate = format.parse(datetimeStr);

Datebase column

date_order_created date

Upvotes: 0

Views: 1967

Answers (3)

happybuddha
happybuddha

Reputation: 1358

From here
The following code formats a date and time according to the pattern String passed to the SimpleDateFormat constructor. The String returned by the format method contains the formatted date and time that are to be displayed.

Date today;
String output;
SimpleDateFormat formatter;

formatter = new SimpleDateFormat(pattern, currentLocale);
today = new Date();
output = formatter.format(today);
System.out.println(pattern + " " + output);
The following table shows the output generated by the previous code ex
Pattern              Output
EEE, MMM d, ''yy    Tue, Jun 30, '09

Upvotes: 1

Thierry
Thierry

Reputation: 5243

When you save a Date objet in a database, this is generally not its String representation which is saved. This depends of the field type used in the database.

And :

Fri May 03 00:38:20 IST 2013 

looks like a date.toString()

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1504122

I hope you're not actually saving your dates as text in the database in the first place. Assuming you're not (and I don't think you are), you're not really "saving date in below format" at all... you're just saving the date.

The value of a date doesn't have a format - June 19th 1976 is the same date whether I represent it that way, or as 1976-06-19 or 19/06/1976 or 06/19/1976 (assuming I know how to interpret any of those formats). That's what you're seeing here - you're saving the date, and when you look at it "in the database" (e.g. via some SQL console) you're seeing one representation... but when you fetch it from the database and print it from Java, you're seeing the results of calling Date.toString(). That doesn't change the value at all.

If you want to format a Date value in one particular way, use SimpleDateFormat - but be very aware that a Date object itself has no format (or time zone, or calendar) and your database probably doesn't either. (The exact details of what goes in the database will depend on the database and the column type.)

It's very important to distinguish between a value and string representations of that value.

For example, if I write:

int x = 0xff;
int y = 255;
System.out.println(x == y);

then that prints "true" - because both x and y have the same value. The fact that in one case I used a hexadecimal representation doesn't affect the value as a number at all.

Upvotes: 5

Related Questions