只是回答自己,所以,它可能幫助一些其他人:
我是有作爲輸入可以說,日期字符串:用於計算時區
String startDate = "2016-04-21T00:00:00+0530"
//i can calculate the timezone offset using
String offSet = startDate.substring(startDate.length() - 5) //gives +0530
方法。這裏,我們給上面的偏移計算下面的方法返回的TimeZone對象:
public static TimeZone fetchTimeZone(String offset) {
if (offset.length() != 5) {
return null
}
TimeZone tz
Integer offsetHours = Integer.parseInt(offset.substring(0, 3))
Integer offsetMinutes = Integer.parseInt(offset.substring(3))
String[] ids = TimeZone.getAvailableIDs()
for (int i = 0; i < ids.length; i++) {
tz = TimeZone.getTimeZone(ids[i])
long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset())
long minutes = Math.abs(TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset()) % 60)
if (hours != offsetHours || minutes != offsetMinutes) {
tz = null
} else {
break
}
}
return tz
}
最後我用的時區從上述方法格式化的任何日期到時區:
SimpleDateFormat timeZonedFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
TimeZone timeZone = fetchTimeZone(offSet) //from above method and offset from first code
timeZonedFormatter.setTimeZone(timeZone);
//this timeZonedFormatter can be used to format any date into the respective timeZone
我想你需要4'Z的那個。 '(「yyyy-MM-dd'T'HH:mm:ssZZZZ)' – Zircon
這是不可能的:多個時區在給定的時刻可以有相同的時間偏移 – assylias
你可能會在這裏找到你的答案:http:/ /stackoverflow.com/questions/1305350/how-to-get-the-current-date-and-time-of-your-timezone-in-java – Akah