将 java.time.LocalDateTime SE 8 转换为时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31110787/
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-11-02 18:07:51  来源:igfitidea点击:

Convert java.time.LocalDateTime SE 8 to timestamp

javamysqltimetimestamp

提问by Josiah L.

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 转换为时间戳?我想使用新的 SE 8 日期 api,因为它比 util 日期和日历更好。我计划在整个程序中使用 localdatetime,然后将该日期放入 mysql 数据库中。我一直在寻找答案,但似乎没有太多关于 java.time 的问题和答案。这是我正在测试的一小部分代码。这是我得到的。

        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?

我想我需要先把它转换成 long ,但我不知道如何。api 允许转换单个元素,如月和日,但不能转换整个日期。既然我已经在这里,你如何从时间戳转换回来?我应该只使用 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.

这只会保存 1970 年左右的日期。 system.print 正确打印当前日期。我希望它保存当前日期。

回答by Wilson Ferro

回答by hammelion

First of all, you should decide if you really want to use LocalDateTime. Below are some explanations about the difference, taken from here:

首先,您应该决定是否真的要使用 LocalDateTime。以下是有关差异的一些解释,摘自此处

<...> LocalDateTime is not a point on the time line as Instant is, LocalDateTime is just a date and time as a person would write on a note. Consider the following example: two persons which were born at 11am, July the 2nd 2013. The first was born in the UK while the second in California. If we ask any of them for their birth date it will look that they were born on the same time (this is the LocalDateTime) but if we align the dates on the timeline (using Instant) we will find out that the one born in California is few hours younger than the one born in the UK (NB: to create the appropriate Instant we have to convert the time to UTC, this is where the difference lays).<...>

<...> LocalDateTime 不像 Instant 那样是时间线上的一个点,LocalDateTime 只是一个人会写在笔记上的日期和时间。考虑以下示例:两个人于 2013 年 7 月 2 日上午 11 点出生。第一个出生在英国,第二个出生在加利福尼亚。如果我们询问他们中的任何人的出生日期,看起来他们是在同一时间出生的(这是 LocalDateTime),但是如果我们将时间线上的日期对齐(使用 Instant),我们会发现出生在加利福尼亚的那个比在英国出生的人年轻几个小时(注意:要创建适当的 Instant,我们必须将时间转换为 UTC,这就是差异所在)。<...>

In order to get long from Instant you could use getEpochSecond()method.

为了从 Instant 获得更长的时间,您可以使用getEpochSecond()方法。

In order to get long from LocalDateTime you should provide a timezone.

为了从 LocalDateTime 中获得更长的时间,您应该提供一个 timezone