0

I am trying to read a timestamp I have in my database mysql and save it to a Date variable in java.

I want it with this format: YYYY-MM-DD HH:MM:SS (24 hour format)

In my database I have a timestamp with that format but each time I try to get as timestamp I read 2014 and if I read it as Date with getDate()... I get "ago 16, 2014"

Biribu
  • 3,615
  • 13
  • 43
  • 79

5 Answers5

0

I'm not sure what you really want. Do you want to get a java.sql.Timestamp instance or do you want to get the timestamp as string with the mentioned pattern?

Maybe that helps:

ResultSet rs = ...
Timestamp t = rs.getTimestamp(...);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(t);
DirkNM
  • 2,614
  • 15
  • 21
  • I am trying your answer right now but I am getting some king of problem when printing hour. If I put hh I get 00 if I put HH I get 12, but my hour for example is 19:... always get or 00 of 12 – Biribu Sep 17 '14 at 11:56
  • HH means Hour in Day (0-23) and hh means Hour in am/pm. It looks like your timestamp value has no hour. Can you tell the exact timestamp value (t.getTime()) – DirkNM Sep 17 '14 at 12:10
  • It is a server and I cannot print it by console but in database I have: 2014-07-07 19:30:00 as timeStamp – Biribu Sep 17 '14 at 14:09
0
// get the timestamp from the DB
java.sql.Timestamp yourTimestamp = youNameItGetTimestamp();

// Create the corresponding Date object
java.util.Date date = new java.util.Date(yourTimestamp.getTime());

// show in a string
java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
java.lang.String printableDate = formatter.format(date);
System.out.println("here you have it: <" + printableDate + ">");
manuelvigarcia
  • 1,696
  • 1
  • 22
  • 32
0

The other Answers are correct but use troublesome old legacy date-time classes. Instead use java.time classes.

Conversion

New methods have been added to the old classes to facilitate conversion such as java.sql.Timestamp::toInstant().

java.sql.Timestamp ts = myResultSet.getTimestamp( … ) ;
Instant instant = ts.toInstant();

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds. Its toString method generates a String in one of the standard ISO 8601 formats.

String output = instant.toString();

2011-12-03T10:15:30Z

You could replace the T with a SPACE and delete the Z to get your format.

output = output.replace( "T" , " ").replace( "Z" , "" ) ;

2011-12-03 10:15:30

ZonedDateTime

If you want to see the same moment but through the lens of a particular time zone, generate a ZonedDateTime.

ZoneId zoneId = ZonedId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( zoneId );

Define a custom DateTimeFormatter object. Or use DateTimeFormatter.ISO_LOCAL_DATE_TIME and replace the T with a SPACE.

String output = zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ).replace( "T" , " " );

2011-12-03 05:15:30

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

have you tried some thing like this :

java.sql.Date timeStamp = new java.sql.Timestamp( object.getDate() );

also this link may help you :

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

omid haghighatgoo
  • 322
  • 1
  • 3
  • 16
-2

What's the type of your field? Is it a string/varchar? How about using SimpleDateFormat?

i.e.

final Date myDate = new SimpleDateFormat(FORMAT_STRING).parse(value);

See SimpleDateFormat documentation for more details.

BTW: A litte code and database definition would have been nice...

Daniel Hiller
  • 3,415
  • 3
  • 23
  • 33