[英]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.