mdev
mdev

Reputation: 472

MySql DATETIME to java.util.Calendar

I managed in JAVA to store a calendar into a mysql DATETIME field To fetch this value

entry.date = Calendar.getInstance(TimeZone.getTimeZone("UT"));
entry.date.setTime(rs.getDate(DBBLogEntries.entDate));

Where the entry.date is a java.util.Calendar In the database the value is this: '2012-07-07 07:18:46' I store all date values in a unique timezone in the db. ready to make all the extra work required to add or substract hours depending on the country from wich the request is comming.

The problem is that it brings the date but doesn't seem to brinng me the time. Any sugestion please? Thanks in advance.

Upvotes: 2

Views: 8051

Answers (4)

Ravinder Reddy
Ravinder Reddy

Reputation: 24012

You should read a timestamp from the ResultSet object.

java.sql.Timestamp ts = rs.getTimestamp( DBBLogEntries.entDate );

Which returns a Timestamp instance that includes date and time.

Upvotes: 1

Lion
Lion

Reputation: 19037

Probably because Java has a different date format than mysql format(YYYY-MM-DD HH:MM:SS)

Visit the link :

http://www.coderanch.com/t/304851/JDBC/java/Java-date-MySQL-date-conversion

You may use SimpleDateFormat as follows.

java.util.Date dt = new java.util.Date();

java.text.SimpleDateFormat sdf = 
     new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String dateTime = sdf.format(dt);

Upvotes: 3

jjathman
jjathman

Reputation: 12624

Are you using the MySql DATE type? This does not preserve the time component.

http://dev.mysql.com/doc/refman/5.5/en/datetime.html

Alternatively how are you retrieving the date from the db?

Upvotes: 0

Blade Master
Blade Master

Reputation: 157

don't use Java.util.Date ,use the Java.sql.Date.

Upvotes: 0

Related Questions