2

私は以下のコードを持っています:

DateFormat df = new SimpleDateFormat("M/d/yy h:mm a z");
df.setLenient(false);
System.out.println(df.parse("6/29/2012 5:15 PM IST"));

PC のタイムゾーンを太平洋時間 (PDT の場合は UTC-7) に設定すると、次のように出力されます。

2012 年 6 月 29 日金曜日 08:15:00 PDT

PDT は IST (インド標準時) から 12.5 時間遅れていませんか? この問題は、他のタイムゾーンでは発生しません。日付文字列で IST の代わりに UTC、PKT、MMT などを試しました。ひょっとして Java には 2 つの IST がありますか?

PS: 実際のコードの日付文字列は外部ソースに由来するため、GMT オフセットやその他のタイムゾーン形式は使用できません。

4

4 に答える 4

10

タイムゾーンの短縮名はあいまいであり、タイムゾーンの Olson 名では推奨されていません。parse() と getTimezone() の動作に違いがある可能性があるため、以下は一貫して機能します。

SimpleDateFormat sdf = new SimpleDateFormat("M/d/yy h:mm a Z");
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
Date d = new Date();
sdf.setTimeZone(istTimeZone);
String strtime = sdf.format(d);
于 2013-05-28T19:45:23.443 に答える
6

申し訳ありませんが、これに対する回答を書く必要がありますが、次のコードを試してください。

public class Test {

    public static void main(String[] args) throws ParseException {
        DF df = new DF("M/d/yy h:mm a z");
        String [][] zs = df.getDateFormatSymbols().getZoneStrings();
        for( String [] z : zs ) {
            System.out.println( Arrays.toString( z ) );
        }
    }

    private static class DF extends SimpleDateFormat {
        @Override
        public DateFormatSymbols getDateFormatSymbols() {
            return super.getDateFormatSymbols();
        }

        public DF(String pattern) {
            super(pattern);
        }
    }

}

IST がリストに何度か表示されていることがわかりますが、最初の 1 つは確かにイスラエル標準時です。

于 2012-06-29T15:59:32.437 に答える
4

答えではありませんが、以下の出力+コードを参照してください-ISTの扱いは...parseとは異なるようです。TimeZone.getTimeZone("IST")

6月29日金曜日16:15:00BST2012年6月29日金曜日12:45:00BST2012年6月29日金曜日12:45:00BST2012
*
BST
=ロンドン

public static void main(String[] args) throws InterruptedException, ParseException {
    DateFormat fmt1 = new SimpleDateFormat("M/d/yy h:mm a Z");
    Date date = fmt1.parse("6/29/2012 5:15 PM IST");
    System.out.println(date);

    DateFormat fmt2 = new SimpleDateFormat("M/d/yy h:mm a");
    fmt2.setTimeZone(TimeZone.getTimeZone("IST"));
    System.out.println(fmt2.parse("6/29/2012 5:15 PM"));

    DateFormat fmt3 = new SimpleDateFormat("M/d/yy h:mm a");
    fmt3.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    System.out.println(fmt3.parse("6/29/2012 5:15 PM"));
}
于 2012-06-29T16:04:11.953 に答える