tags:

views:

208

answers:

3

Hi I do a rs.getTimestamp("datetime")

in Java.

In the database, this datetime is 2009/03/06 02:47:18

but the time is returned as 14:47:18

I'm not very knowledgeable about MySQL dates, so I would appreciate any explanation as to why this is happening.

+1  A: 

Two things. First, I think we need sample code. What's going on is not at all clear from what you've given us. Context, usage, DB schema, and sample rows as well.

Second, ResultSet.getTimestamp() should be returning an object of type Timestamp, rather than a String of any sort.

Kalium
+2  A: 

It doesn't matter. Its not about MySQL or any database. This is the format Timestamp shows up by default, I believe. It doesn't mean it missed the date or something.

You can always format the Timestamp returned by the method in any format in your code. Check out java.text.SimpleDateFormat class. Or for better, check out much more sophisticated Joda Time.

Adeel Ansari
I format it using a SimpleDateFormatter, using HHmmss as my format.
kilhra
Try small 'h'. Something like "hhmmss a"
Adeel Ansari
For exact as MySQL, try "yyyy/MM/dd hh:mm:ss"
Adeel Ansari
I'll try that, but I might add that I do not get this problem if I use SQL Server
kilhra
It worked, thanks, I'm not sure if it will work in the long run though.
kilhra
And why is this doubt?
Adeel Ansari
as far as I can see, mySQL uses 12 hour time, and I don't know how to check whether it is am or pm. I need to use the time to find records, what if there are two times, looking exactly the same, but one am and one pm?
kilhra
Try "yyyy/MM/dd hh:mm:ss a".
Adeel Ansari
MySQL knows and has whatever it needs to know whether its AM or PM. May be you can configure your client to show that, or use some other client. Or I am afraid if you need to expand that column a bit to make that visible.
Adeel Ansari
Thank you for your help
kilhra
A: 

SimpleDateFormat time = new SimpleDateFormat("HHmmss");

datime = time.format(rs.getTimestamp("datetime"))

and then datime is printed to a file.

the datetime column in the table is a datetime Data Type

kilhra
For exact as MySQL, try "yyyy/MM/dd hh:mm:ss"
Adeel Ansari