-1


Androidフォームでは、+5:30、+3:00などのユーザーからGMT値(オフセット)を受け入れています。
この値から、「インド/デリー」である timeZone を計算したいと思います。

それを行う方法についてのアイデア......Plzz

4

3 に答える 3

4

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.

于 2013-05-11T08:02:59.350 に答える
-1

あなたの質問を正しく解釈した場合は、これを試してください。

final SimpleDateFormat date=
        new SimpleDateFormat("EEE, MMM d, yyyy hh:mm:ss a z");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("GMT time: " + date.format(currentTime));

これで、オフセットを追加できます。これが役立つかどうかを確認してください。

于 2013-05-11T07:44:00.433 に答える