java - 如何在java或JODA中获取另一个时区的时间戳

标签 java jodatime

我想从不同时区获取当前时间。

例如使用 joda 日期时间库,

我可以像使用 JODA datetime 一样获取澳大利亚时区

DateTime zoned = new DateTime(DateTimeZone.forID("Australia/Melbourne"));

及其当前时间使用

DateTime.now(DateTimeZone.forID("Australia/Melbourne"));

如果我想将此 DateTime 对象转换为 java.sql.Timestamp 对象

,我必须使用它的毫秒数

DateTime 类的 getMillis 方法实例化新的时间戳对象

Timestamp zonedStamp = new TimeStamp(zoned.getMillis());

因此,每次自纪元时间以来经过的毫秒数在逻辑上对于每个时区都是相同的。

我的问题是如何获取澳大利亚时区的当前时间来获取分区时间戳对象。

谢谢你 米希尔·帕雷赫

最佳答案

如果您想要一个具有澳大利亚时区等效时间值Timestamp对象,请尝试以下操作:

    Date currentTime = new Date();
    DateFormat ausFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    ausFormat.setTimeZone(TimeZone.getTimeZone("Australia/Melbourne"));

    //get the time string in australian timezone
    String ausTime  = ausFormat.format(currentTime);

    //Convert the above time string in local date object
    DateFormat currentFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    //optional: set the timezone as Asia/Calcutta
    currentFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
    Date ausTimeInLocal = currentFormat.parse(ausTime);

    //get the time stamp object using above date object
    Timestamp ausTimeStampInLocal = new Timestamp(ausTimeInLocal.getTime());

关于java - 如何在java或JODA中获取另一个时区的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13539028/

相关文章:

java - 如何使用 JodaTime 解析格式 "HHmm"的持续时间?

java - 自定义 Joda-Time Chronologies 是否有好的资源?

java - 我可以用 JodaTime 解析相对时间吗?

java.lang.OutOfMemory错误: Java heap space

java - 卸载应用程序时执行操作

java - 如何检查wordNet中是否有单词

java - 用包列表填充树的快速而肮脏的方法

java - 解析来自不同时区的时间戳字符串

java - JodaTime IllegalArgumentException 格式无效 "yyyy-MM-dd hh:mm:ss"

javascript - Java servlet 和 javascript get 请求 : multiple requests to the same servlet, 更新了游戏状态