198

In Java 8, how can I convert a Timestamp (in java.sql) to a LocalDate (in java.time)?

1

3 Answers 3

303

You can do:

timeStamp.toLocalDateTime().toLocalDate();

Note that timestamp.toLocalDateTime() will use the Clock.systemDefaultZone() time zone to make the conversion. This may or may not be what you want.

Sign up to request clarification or add additional context in comments.

7 Comments

It's been added in Java 8.
Just a note, the timestamp.toLocalDateTime() method will use the systemDefault time zone to make the conversion. This may or may not be what you want.
@jacobhyphenated please post answer using explicit time zone. Thanks!
@jacobhyphenated's comment is extremely important here, hence the number of upvotes for it. See my answer for details stackoverflow.com/a/57101544/2032701
@jacobhyphenated, LocalDateTime always uses system-default timezone. That's what "Local" means in its name.
|
63

The accepted answer is not ideal, so I decided to add my 2 cents

timeStamp.toLocalDateTime().toLocalDate();

is a bad solution in general, I'm not even sure why they added this method to the JDK as it makes things really confusing by doing an implicit conversion using the system timezone. Usually when using only java8 date classes the programmer is forced to specify a timezone which is a good thing.

The good solution is

timestamp.toInstant().atZone(zoneId).toLocalDate()

Where zoneId is the timezone you want to use which is typically either ZoneId.systemDefault() if you want to use your system timezone or some hardcoded timezone like ZoneOffset.UTC

The general approach should be

  1. Break free to the new java8 date classes using a class that is directly related, e.g. in our case java.time.Instant is directly related to java.sql.Timestamp, i.e. no timezone conversions are needed between them.
  2. Use the well-designed methods in this java8 class to do the right thing. In our case atZone(zoneId) made it explicit that we are doing a conversion and using a particular timezone for it.

7 Comments

I disagree. Timestamp (and also Date) do not have any zone information. LocalDate does not have any zone information so the two are equivalent. Conversion between them can be done independently of any particular zone. Converting to a ZonedDateTime before converting to LocalDate adds in a timezone and then takes it off again - you're much more likely to get a nasty -hard to find- error doing it that way.
@AutomatedMike Timestamp (and also Date) represent a point in time in UTC. LocalDate does not represent a point in time, but rather a time as seen on a wall clock. Please read their javadocs. "Conversion between them can be done independently of any particular zone" - not true, new Timestamp(0L).toLocalDateTime() returns "1970-01-01T03:00" for my Moscow timezone. The result may be different for you timezone.
@AutomatedMike Even if you strip the time by doing toLocalDate() it's easy to prove that the day can be wrong if Timestamp had a time around midnight, for example new Timestamp(1000 * 60 * 60 * 23).toLocalDateTime().toLocalDate() returns "1970-01-02" for my Moscow timezone when it's "1970-01-01" in UTC.
@AutomatedMike "adds in a timezone and then takes it off again" - the point is not adding and removing a timezone, but rather converting between different notions by using an explicit timezone. This is in contrast to the examples of my previous two comments where a timezone is applied implicitly.
When you get a TIMESTAMP (WITHOUT TIME ZONE) from the database it is effectively a LocalDateTime though. Then if you're mapping it to a java.sql.Timestamp your JDBC driver converts the local date/time data to a number of milliseconds using the system default TimeZone. .toLocalDateTime() uses the default timezone again to convert the milliseconds to a LocalDateTime. This seems relatively safe to me.
|
8

I'll slightly expand @assylias answer to take time zone into account. There are at least two ways to get LocalDateTime for specific time zone.

You can use setDefault time zone for whole application. It should be called before any timestamp -> java.time conversion:

public static void main(String... args) {
    TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
    TimeZone.setDefault(utcTimeZone);
    ...
    timestamp.toLocalDateTime().toLocalDate();
}

Or you can use toInstant.atZone chain:

timestamp.toInstant()
        .atZone(ZoneId.of("UTC"))
        .toLocalDate();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.