如何将 java.sql.timestamp 转换为 LocalDate (java8) java.time?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23263490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:34:27  来源:igfitidea点击:

How to convert java.sql.timestamp to LocalDate (java8) java.time?

javatimestampjava-8localtimejava-time

提问by simonides

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

在 Java 8 中,如何将Timestamp(in java.sql) 转换为LocalDate(in java.time)?

采纳答案by assylias

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.

请注意,timestamp.toLocalDateTime()将使用Clock.systemDefaultZone()时区进行转换。这可能是也可能不是您想要的。

回答by Pavel

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.

我将稍微扩展@assylias 的答案以将时区考虑在内。至少有两种方法可以获取特定时区的 LocalDateTime。

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

您可以对整个应用程序使用 setDefault 时区。它应该在任何时间戳 -> java.time 转换之前调用:

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

Or you can use toInstant.atZone chain:

或者你可以使用 toInstant.atZone 链:

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

回答by Ruslan

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

接受的答案并不理想,所以我决定加上我的 2 美分

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.

总的来说,这是一个糟糕的解决方案,我什至不确定他们为什么将这种方法添加到 JDK 中,因为它通过使用系统时区进行隐式转换使事情变得非常混乱。通常当只使用 java8 日期类时,程序员被迫指定一个时区,这是一件好事。

The good solution is

好的解决办法是

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

Where zoneIdis 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

其中zoneId是您要使用的时区,如果您想使用系统时区或某些硬编码时区(如ZoneOffset.UTC ,则通常是ZoneId.systemDefault()

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.
  1. 使用直接相关的类打破新的 java8 日期类,例如在我们的例子中java.time.Instant 与 java.sql.Timestamp 直接相关,即它们之间不需要时区转换。
  2. 使用这个 java8 类中设计良好的方法来做正确的事情。在我们的例子中,atZone(zoneId)明确表示我们正在进行转换并为其使用特定的时区。