• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert java.time.LocalDateTime SE 8 to timestamp

 
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:
time (Etc/UTC) = 2015-06-29 14:38:57.168
time (Europe/Brussels) = 2015-06-29 12:38:57.168


Hello Roel,

I appreciate your explanation but in my opinion something must have gone wrong with your solution since the times are the other way round they should be: Brussels is one hour _ahead_ of UTC (two if DST applies). But in your output Brussels is behind UTC which is obviously not true.
Could you check please? Or am I understanding something wrong?

Thank you!
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch I tried that and also got the hour difference the wrong way round. We shall have to wait for Roel to notice.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic