简体   繁体   中英

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

I have a Java application which uses a MyBatis TypeHandler class. It needs to make sure that the date being stored into the MySql database is in MST time.

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. 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):

2016-05-05T07:01:00.000Z

The app side does all date comparisons in UTC, but unfortunately, the MySql server must store in 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.

The setParameter method of the UtcToMstDateTimeTypeHanlder:

@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:

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.

The desired effect is to have the TypeHandler write the date back to the database as the following 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).

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. The basic idea is to get the offset from the time zone.

From the DateTimeZone JodaTime Class:

getOffset(long instant) Gets the millisecond offset to add to UTC to get local time.

        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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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