Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert java.time.LocalDateTime SE 8 to timestamp

 
Jose Livens
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you convert a Localdatetime to timestamp? I want to use the new SE 8 date api because it is better than the util date and calendar. I plan to use localdatetime throughout my program and then place that date into a mysql database. I have looked for an answer but there doesn't seem to be very many questions and answers for java.time. This is a little of the code that I am testing. This is as far as I got.

LocalDateTime c = LocalDateTime.now();

java.sql.Timestamp javaSqlDate = new java.sql.Timestamp(c.getLong());

I think I need to convert it into a long first, but I don't know how. The api allows for converting individual elements like month and day, but not for the whole date. Since I'm already here, how do you convert back from timestamp? Should I just use jodatime?

I tried this:

LocalDateTime c = LocalDateTime.now();
ZoneId zoneId = ZoneId.systemDefault();

System.out.println("this:" + c);

java.sql.Timestamp javaSqlDate = new java.sql.Timestamp(c.atZone(zoneId).toEpochSecond());

pst.setTimestamp(2, javaSqlDate);

This only saves the date around 1970. The system.print prints the current date correctly. I want it to save the current date.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jose Livens,

First of all, a warm welcome to CodeRanch!

Jose Livens wrote:How do you convert a Localdatetime to timestamp?

Always use the API if possible! It makes your life a lot easier

Have a look at the Timestamp javadoc (of Java 8) and you'll notice some methods have been added which convert from LocalDateTime to Timestamp and vice versa.

Hope it helps!
Kind regards,
Roel
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15631
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Jose.

The reason why you cannot translate a LocalDateTime to a timestamp directly is because a LocalDateTime is not an absolute point on the timeline - it is a date and time, but to know what absolute point in time it is, you need to have additional information - the timezone in which to interpret the date and time.

You almost got the right solution with this:

except that you need to give the constructor of Timestamp milliseconds and not seconds since the epoch (01-01-1970, 00:00:00 UTC).

You could do this by simply multiplying by 1000, if you don't care about the fractions of a second:


Or you can do this, which will preserve the fractions of a second:


*edit* - Roel's solution is even better, it turns out that class java.sql.Timestamp has a factory method to create a Timestamp from a LocalDateTime:

Note that this will interpret the LocalDateTime in your system timezone (which is what you were doing anyway).
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Note that this will interpret the LocalDateTime in your system timezone (which is what you were doing anyway).

And if you want to use a timezone different from the system timezone, you can use similar code with a little extra work to incorporate the specific time zone.

Some demo codeOutput:
time (Etc/UTC) = 2015-06-29 14:38:57.168
time (Europe/Brussels) = 2015-06-29 12:38:57.168


Hope it helps!
Kind regards,
Roel
 
Tim Holloway
Saloon Keeper
Posts: 18362
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timestamps are treacherous little beasties. The granularit(ies) available for a Timestamp within the database itself depend on the brand, version and schema definition. And that's even before you consider timezones, which can be unsupported, implicitly supported or explicitly determined - again, determined by brand, version and schema.

I prefer to treat them like floating-point numbers and NEVER compare on exact-match or use them for primary keys.

The stock java.sql.Date and java.util.Date values are precise to milliseconds and are adequate to map to database TimeStamp objects in most cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic