我想从不同时区获取当前时间。
例如使用 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/