简体   繁体   English

在另一个时区将UTC JodaTime.DateTime对象转换为TimeStamp

[英]Converting UTC JodaTime.DateTime Object to TimeStamp in another Time Zone

I have a Java application which uses a MyBatis TypeHandler class. 我有一个使用MyBatis TypeHandler类的Java应用程序。 It needs to make sure that the date being stored into the MySql database is in MST time. 它需要确保存储在MySql数据库中的日期是MST时间。

Before any data gets to the app, we have another TypeHandler that takes the date from the database, which is in MST, and converts it to UTC. 在任何数据到达应用程序之前,我们有另一个 TypeHandler从数据库获取日期,该日期在MST中,并将其转换为UTC。 So, for example, in the database if the timestamp was: 因此,例如,在数据库中,如果时间戳是:

2016-05-05 00:01:00

when the date appears on the app side it is in the following format (UTC): 当日期出现在应用程序端时,它采用以下格式(UTC):

2016-05-05T07:01:00.000Z

The app side does all date comparisons in UTC, but unfortunately, the MySql server must store in MST. 应用程序端以UTC格式进行所有日期比较,但不幸的是,MySql服务器必须存储在MST中。

In order to keep dates consistent from whichever server the app is running (it is run in MST, PST as well as EST) we will need the two TypeHandlers, one to marshall the date coming into the app and one to make sure it's in MST going back. 为了保持日期与应用程序运行的任何服务器(它在MST,PST以及EST中运行)保持一致,我们将需要两个TypeHandler,一个用于编组进入应用程序的日期,另一个用于确保它在MST中回去。

The setParameter method of the UtcToMstDateTimeTypeHanlder: UtcToMstDateTimeTypeHanlder的setParameter方法:

@Override
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException
{
    if (parameter != null)
    {
        //1.  2016-05-05 00:01:00 (timestamp) converted to UTC DateTime -> 2016-05-05T07:01:00.000Z
        DateTime thisDateTime = (DateTime) parameter;
        //2.  UTC DateTime converted to LocalDateTime -> 2016-05-05T07:01:00.000
        LocalDateTime thisLocalDateTime = thisDateTime.toLocalDateTime();
        //3.  LocalDateTime to MST DateTime -> 2016-05-05T07:01:00.000-07:00
        DateTime mstTime = thisLocalDateTime.toDateTime(DateTimeZone.forID("MST"));
        //4.  But TimeStamp adds 3 hours... Why?  2016-05-05 10:01:00.0
        Timestamp mstTimeStamp = new Timestamp((mstTime).getMillis());

        ps.setTimestamp(i, mstTimeStamp);
    }
    else
    {
        ps.setTimestamp(i, null);
    }
}

The TimeStamp ends up being 3 hours ahead of UTC: TimeStamp最终比UTC早3个小时:

2016-05-05 10:01:00.0

Not only that, but it is also more relative to UTC than MST, except now +10:01 hours ahead of UTC. 不仅如此,它还比UTC更加相对于UTC,除了现在比UTC早10:01小时。

The desired effect is to have the TypeHandler write the date back to the database as the following TimeStamp: 期望的效果是让TypeHandler将日期作为以下TimeStamp写回数据库:

2016-05-05 00:01:00.0

I would simply like to have the date provided back to the database (the timestamp above) to be the same as what it came out as. 我只想将提供给数据库的日期(上面的时间戳)与它的结果相同。

Note that right now I'm running this on the United States east coast (EST). 请注意,我现在正在美国东海岸(EST)运行。

Here's what I ended up having to do. 这就是我最终要做的事情。 Note below the hard-coding of the date that I was working with, coming in from the UTC to MST typehandler. 请注意下面我正在使用的日期的硬编码,从UTC到MST类型处理程序。 The basic idea is to get the offset from the time zone. 基本思路是从时区获得偏移量。

From the DateTimeZone JodaTime Class: 来自DateTimeZone JodaTime类:

getOffset(long instant) Gets the millisecond offset to add to UTC to get local time. getOffset(long instant)获取要添加到UTC以获取本地时间的毫秒偏移量。

        DateTime thisDateTime = new DateTime("2016-05-07T07:01:00.000Z");

        thisDateTime = thisDateTime.withZone(DateTimeZone.UTC);

        thisDateTime = thisDateTime.toLocalDateTime().toDateTime();

        DateTime mstTime = thisDateTime.withZone(DateTimeZone.forID("MST"));    

        int offset = mstTime.getZone().getOffset(new DateTime().getMillis());

        Timestamp mstTimeStamp = new Timestamp(mstTime.getMillis() + offset); // -25200000 == 7 hours

        ps.setTimestamp(i, mstTimeStamp); //2016-05-07 00:01:00

Now, no matter where the server running the app code is, the date is always entered correctly into the database, which runs in MST time. 现在,无论运行应用程序代码的服务器在哪里,日期总是正确输入数据库,该数据库在MST时间运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM