[英]how to get a timestamp of another timezone in java or JODA
我想从其他时区获取当前时间(现在)。
例如使用joda datetime库,
我可以像使用JODA datetime一样获得澳大利亚时区
DateTime zoned = new DateTime(DateTimeZone.forID("Australia/Melbourne"));
及其当前时间
DateTime.now(DateTimeZone.forID("Australia/Melbourne"));
如果我想将此DateTime对象转换为java.sql.Timestamp对象
,我必须使用毫秒
DateTime类的getMillis方法实例化新的Timestamp对象
Timestamp zonedStamp = new TimeStamp(zoned.getMillis());
因此从纪元时间开始经过的毫秒数在每个时区在逻辑上都是相同的。
我的问题是我如何获取澳大利亚时区的当前时间以获取分区的时间戳记对象。
谢谢你Mihir Parekh
如果要使用澳大利亚时区等效时间值的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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.