• 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

Converting between LocalDate and java.util.Date

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recently attempted to convert a large code base to use java.time classes such as LocalDate, instead of the old java.util.Date classes. However it wasn't possible to convert everything because the code has to interface with other code which still uses the legacy java.util classes. Such as javax.swing.SpinnerDateModel, which wants a Calendar object. So there still has to be code which translates between the two styles.

That turned out to be harder than I expected, and here's the code I came up with:



So, two questions:

1. Is that really the simplest way to do the conversion?

2. I'm tempted to try using java.sql.Date objects throughout, after all a java.sql.Date object is a java.util.Date object. And the conversion is a whole lot easier:



Okay, that wasn't a question but I'd still like comments on it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was going to answer with the same thing you came up with, so I'd say it's the obvious way to go.

The thing is, a LocalDate is different all over the world. While it's now February 7th in Europe, in other parts of the world it's already February 8th. Since both Instant and Date are more or less timestamps it's easy to convert these two to the other. For the local classes (LocalDate, LocalTime and LocalDateTime) you first must convert these to something that's also a timestamp. For all three that means picking a time zone; for LocalDate it also means picking a time, and for LocalTime it's also picking a date. In short: what you already have.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I know about the time zone thing, but I was hoping that there were some convenience methods which I had overlooked, something which automatically used the default time zone which didn't have to be told to do so. Something which defaulted to using the default time zone, in other words.

Anyway the SQL date idea works quite well because usually the dates are coming from a DATE column in the database. (I was hoping that ResultSet would have had a getLocalDate() method added in Java 8 but apparently not.) Unfortunately the times are coming from a 4-digit integer column in the database which rightly should be a TIME column but I didn't do that when I created the database all those years ago. I should probably convert those columns.

Thanks.
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic