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;
}
}
On my box that prints:
Asia/Calcutta
Asia/Colombo
Asia/Kolkata
IST
(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;
}
... 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…