我想從給定的時區獲得當前時間,可以像「IST」或「印度標準時間」等。 當輸入爲「印度標準時間」或「協調世界時」時,我無法獲得時間。它只適用於「IST」或「UTC」或「EST」等。 我已經嘗試過Joda-Time和SimpleDateFormat。如何從java中給定的時區名稱獲取當前時間?
SimpleDateFormat sd = new SimpleDateFormat(
"yyyy.MM.dd G 'at' HH:mm:ss z");
Date date = new Date();
sd.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(sd.format(date));
DateTime now = new DateTime();
//DateTimeZone LDateTimeZone = DateTimeZone.forID("Asia/Kolkata");
DateTimeZone LDateTimeZone = DateTimeZone.forID("IST"); //deprecated
System.out.println("Joda-Time zone: " + now.toDateTime(LDateTimeZone));
有沒有辦法處理這兩個輸入?
始終使用全IANA時區信息數據庫的名字 - '亞洲/ Kolkata'。縮寫不是唯一的(印度/以色列/愛爾蘭?),人性化名稱是語言/地區特定的。 –