如何将特定的日期值强制设置为时间戳记? [英] How Do I Force A Specific Date Value into a Timestamp?

查看:68
本文介绍了如何将特定的日期值强制设置为时间戳记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个日期值,我必须将其转换为时间戳,以便将该值传递给特定的方法.但是,为了完成工作,我们被告知时间戳记日期的值必须设置为1900-01-01.

So I have a date value I had to convert to a timestamp in order to pass the value to a specific method. However, in order to finish the work, I've been told the value of the timestamp's date has to be set to 1900-01-01.

像这样

        Date startDate = timesDate.getStartDate(); 
        Timestamp startTime = new Timestamp(startDate.getTime());

但是现在我必须将日期固定为该特定值,同时保持原样.

But now I have to fix the date to that specific value, while keeping the time what it was.

编辑-我必须从包含具有设置时间的字符串的字段中提取数据.

EDIT - I have to pull data from a field that contains a string with a set time.

这将是一个字段的开始时间,另一个字段的结束时间.保存该字符串后,需要将其转换为时间戳,并且时间戳的值需要在时间值的旁边加上1900-01-01作为前缀.

This will be a start time for one field and an end time for another. When this is saved the string needs to be converted into a timestamp, and the value of the timestamp need to be prefaced with 1900-01-01 alongside the time value.

我可以回答自己的问题吗?

CAN I ANSWER MY OWN QUESTION?

推荐答案

tl; dr

从旧版 java.util.Date 对象开始,更改日期,同时保持UTC所示的时间.

tl;dr

Starting with a legacy java.util.Date object, change the date while keeping the time-of-day as seen in UTC.

myJavaUtilDate                                 // Your `java.util.Date` object. Now legacy; avoid!
.toInstant()                                   // Convert fromlegacy class to modern class, `Instant`.
.atOffset( ZoneOffset.UTC )                    // Convert from basic building-block class `Instant` to the more flexible class `OffsetDateTime`. 
.with(                                         // Move from one date to another. 
    LocalDate.of( 1900 , Month.JANUARY , 1 )   // Specify date while keeping time-of-day, and keeping the same offset-from-UTC.
)                                              // Returns a second `OffsetDateTime` object, per immutable objects. 

您可以使用JDBC 4.2或更高版本将生成的 OffsetDateTime 对象传递到数据库,而无需使用 Timestamp ,如下所述.

You can pass the resulting OffsetDateTime object to your database using JDBC 4.2 or later, with no need for Timestamp as explained below.

有人告诉我时间戳记的日期值必须设置为1900-01-01.

I've been told the value of the timestamp's date has to be set to 1900-01-01.

我怀疑您对

I suspect you are confused about the oddity in the deprecated constructor for java.sql.Timestamp that counts year from 1900. The Javadoc describes the first argument as: year - the year minus 1900.

那将是遗留日期时间类中发现的许多螺丝设计缺陷之一.完全避免这些类.仅使用 java.time 包中的日期时间类.

That would be one of the many screwy design flaws found in the legacy date-time classes. Avoid these classes entirely. Use only date-time classes from the java.time packages.

如果给定一个 java.util.Date 对象,请立即将其转换为其替换 Instant .

If given a java.util.Date object, immediately convert to its replacement Instant.

Instant instant = myJavaUtilDate.toInstant() ;

如果必须生成一个 java.sql.Timestamp 对象时间与尚未更新为 java.time 的旧代码进行互操作,则可以进行转换.

If you must produce a java.sql.Timestamp object time interoperate with old code not yet updated to java.time, you can convert.

java.sql.Timestamp ts = Timestamp.from( instant ) ;

您不应再使用 Timestamp 与数据库交换值.JDBC 4.2和更高版本要求支持某些 java.time 类,包括 OffsetDateTime .

You should no longer be using Timestamp to exchange values with a database. JDBC 4.2 and later requires support for some of the java.time classes including OffsetDateTime.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
myPreparedStatement.setObject( … , odt ) ;

检索.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ; 

您说:

但是现在我必须将日期固定为该特定值,同时保持原样.

But now I have to fix the date to that specific value, while keeping the time what it was.

您在那里失去了我.编辑您的问题以阐明您的问题和期望的结果.如果您引用的是上面讨论的1900奇数,如果您使用 java.time 类遵循我的代码,那么这是没有问题的.

You lost me there. Edit your Question to clarify your problem and desired outcome. If you are referring the 1900 oddity discussed above, that is moot, a non-issue, if you follow my code using the java.time classes.

如果您确实希望保留1900年1月1日的日期,同时又要在传递的 java.util.Date 中找到一天中的时间,则必须首先了解 Date 对象代表UTC中的时刻.因此,如果通过另一个时区查看同一时刻,那么在UTC中看到的一天中的时间可能会有所不同.

If you really want the date of January 1st 1900 while keeping the time of day found in the passed java.util.Date, you must first understand that the Date object represents a moment in UTC. So the time of day seen in UTC may be differ if viewing that same moment through another time zone.

我将继续假设您想要UTC所示的一天中的时间.但是您应该编辑问题以阐明UTC与某些时区的差异.

I will continue assuming you want the time of day as seen in UTC. But you should edit your Question to clarify UTC versus some time zone.

LocalDate ld = LocalDate.of( 1900 , Month.JANUARY , 1 ) ;
OffsetDateTime odt = myJavaUtilDate.toInstant().atOffset( ZoneOffset.UTC ).with( ld ) ;

这篇关于如何将特定的日期值强制设置为时间戳记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆