如何在Java或JODA中获取另一个时区的时间戳 [英] how to get a timestamp of another timezone in java or JODA
问题描述
我想从其他时区获取当前时间(现在).
i want to get current time (now) from Different time zone .
例如使用joda datetime库,
for example using joda datetime library,
我可以像使用JODA datetime一样获得澳大利亚时区
I can get Australian time zone like using JODA datetime
DateTime zoned = new DateTime(DateTimeZone.forID("Australia/Melbourne"));
及其当前时间使用
DateTime.now(DateTimeZone.forID("Australia/Melbourne"));
如果我想将此DateTime对象转换为java.sql.Timestamp对象
if i want to convert this DateTime object into java.sql.Timestamp object
,我必须使用它来获取毫秒数
,i have to get its milliseconds using
getMillis方法实例化新的Timestamp对象
getMillis method of DateTime class to instantaite new Timestamp Object
Timestamp zonedStamp = new TimeStamp(zoned.getMillis());
因此,从纪元时间开始经过的每毫秒都在逻辑上对于每个时区都是相同的.
so every time the passed milliseconds since the epoch time would be the same logically for each timezone.
我的问题是如何获取澳大利亚时区的当前时间以获取分区的时间戳记对象.
谢谢 米希尔·帕雷克(Mihir Parekh)
Thank You Mihir Parekh
推荐答案
如果您要使用澳大利亚时区等效时间值的Timestamp
对象,请尝试以下操作:
If you want a Timestamp
object with Australian timezone equivalent time value then try below:
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或JODA中获取另一个时区的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!