从格林尼治标准时间值计算时区 - Android电子 [英] Calculating timezone from GMT value - Android
问题描述
在机器人的形式,我接受GMT值(偏移)从用户这样一个+5:30,+ 3:00。
从这个价值,我想计算时区是印度/新德里。
In a android form , i am accepting a GMT value(offset) from user such a +5:30 , +3:00.
and from this value , i want to calculate the timeZone that is "India/Delhi".
这是如何做到这一点的任何想法...... Plzz
Any ideas on how to do it ......Plzz
推荐答案
如果您已经在某一特定时刻着该偏移是有效的,你可以做这样的事情:
If you already have a specific instant in time at which that offset is valid, you could do something like this:
import java.util.*;
public class Test {
public static void main(String [] args) throws Exception {
// Five and a half hours
int offsetMilliseconds = (5 * 60 + 30) * 60 * 1000;
for (String id : findTimeZones(System.currentTimeMillis(),
offsetMilliseconds)) {
System.out.println(id);
}
}
public static List<String> findTimeZones(long instant,
int offsetMilliseconds) {
List<String> ret = new ArrayList<String>();
for (String id : TimeZone.getAvailableIDs()) {
TimeZone zone = TimeZone.getTimeZone(id);
if (zone.getOffset(instant) == offsetMilliseconds) {
ret.add(id);
}
}
return ret;
}
}
在我的机器,打印:
Asia/Calcutta
Asia/Colombo
Asia/Kolkata
IST
(据我所知,印度/新德里不是一个有效的时区信息ID)。
(As far as I'm aware, India/Delhi isn't a valid zoneinfo ID.)
如果你不知道一个即时在该偏移是有效的,这变得相当难的真正的做正确。这里有一个版本:
If you don't know an instant at which the offset is valid, this becomes rather harder to really do properly. Here's one version:
public static List<String> findTimeZones(int offsetMilliseconds) {
List<String> ret = new ArrayList<String>();
for (String id : TimeZone.getAvailableIDs()) {
TimeZone zone = TimeZone.getTimeZone(id);
if (zone.getRawOffset() == offsetMilliseconds ||
zone.getRawOffset() + zone.getDSTSavings() == offsetMilliseconds) {
ret.add(id);
}
}
return ret;
}
...但假定有每个时区只有永远两个偏移的时候,其实时区可以在历史相当大的变化。它也给你一个更广泛的ID,当然。例如,一小时的偏移将包括欧洲/伦敦和欧洲/巴黎,因为在夏季时间伦敦为UTC + 1,而在冬季巴黎是UTC + 1
... but that assumes that there are only ever two offsets per time zone, when in fact time zones can change considerably over history. It also gives you a much wider range of IDs, of course. For example, an offset of one hour would include both Europe/London and Europe/Paris, because in summer time London is at UTC+1, whereas in winter Paris is at UTC+1.
这篇关于从格林尼治标准时间值计算时区 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!