Joda DateTime到时间戳转换 [英] Joda DateTime to Timestamp conversion

查看:760
本文介绍了Joda DateTime到时间戳转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Joda 中的 DateTimeZone 更改 Timestamp 的值:

I am trying to change the value of Timestamp by DateTimeZone in Joda :

  DateTime dt = new DateTime(rs.getTimestamp("anytimestampcolumn"),
                             DateTimeZone.forID("anytimezone"));
  Timestamp ts = new Timestamp(dt.getMillis());




  • 对于 DateTime ,价值是: 2013-04-13T22:56:27.000 + 03:00

    对于 TimeStamp ,值为: 2013-04-13 22:56:27.0

    时间戳即将推出,没有时区差异。

    Timestamp is coming without timezone difference.

    如何使用TimeZone获得正确的时间戳?
    例如我想得到2013-05-13 01: 56:27.0。

    How can i get the correct Timestamp with TimeZone ? For example I want to get "2013-05-13 01:56:27.0".

    提前致谢。

    编辑使用 MySQL ,列类型是 TIMESTAMP 当然, rs ResultSet

    edit : using MySQL, column type is TIMESTAMP of course, rs is ResultSet.

    推荐答案

    实际上这不是一个重复的问题。这就是我多次解决问题的方法:

    Actually this is not a duplicate question. And this how i solve my problem after several times :

       int offset = DateTimeZone.forID("anytimezone").getOffset(new DateTime());
    

    这是从所需时区偏移的方法。

    This is the way to get offset from desired timezone.

    让我们回到我们的代码,我们从查询结果集中获取时间戳,并将其与时区一起使用来创建我们的日期时间。

    Let's return to our code, we were getting timestamp from a result set of query, and using it with timezone to create our datetime.

       DateTime dt = new DateTime(rs.getTimestamp("anytimestampcolumn"),
                             DateTimeZone.forID("anytimezone"));
    

    现在我们将偏移量添加到日期时间,并从中获取时间戳。

    Now we will add our offset to the datetime, and get the timestamp from it.

        dt = dt.plusMillis(offset);
        Timestamp ts = new Timestamp(dt.getMillis());
    

    可能这不是获得它的实际方法,但它解决了我的情况。我希望它可以帮助那些被困在这里的人。

    May be this is not the actual way to get it, but it solves my case. I hope it helps anyone who is stuck here.

    这篇关于Joda DateTime到时间戳转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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