194

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

1

3 Answers 3

298

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.

7
  • 1
    It's been added in Java 8.
    – assylias
    Commented Apr 24, 2014 at 9:13
  • 77
    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. Commented Apr 24, 2014 at 12:53
  • 1
    @jacobhyphenated please post answer using explicit time zone. Thanks!
    – user482745
    Commented Jun 19, 2018 at 8:58
  • 1
    @jacobhyphenated's comment is extremely important here, hence the number of upvotes for it. See my answer for details stackoverflow.com/a/57101544/2032701
    – Ruslan
    Commented Jul 18, 2019 at 19:40
  • @jacobhyphenated, LocalDateTime always uses system-default timezone. That's what "Local" means in its name. Commented Aug 26, 2019 at 11:07
62

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
  • 2
    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. Commented Aug 13, 2019 at 11:23
  • 9
    @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.
    – Ruslan
    Commented Aug 14, 2019 at 12:14
  • 1
    @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.
    – Ruslan
    Commented Aug 14, 2019 at 12:14
  • 1
    @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.
    – Ruslan
    Commented Aug 14, 2019 at 12:16
  • 1
    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.
    – Yay295
    Commented Oct 1, 2021 at 22:02
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();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.