1

Indigo Service Release 2 を使用しています。次のコードを記述しました。

TimeZone calcutta = TimeZone.getTimeZone("Asia/Calcutta");
Date now = new Date(); 
DateFormat format = 
    DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);  
format.setTimeZone(calcutta);  
jlabel_IndiaTime.setText((format.format(now).toString()));

が表示されてMonday, September 17,2012 1:13:23 PM ISTいますが、インドの時刻は午前 10 時 14 分です。私はニューヨークからこれを試しています。誰でも私を助けてもらえますか?

4

2 に答える 2

0

以下のような SimpleDateFormat および Date クラスを使用できます。

    Date date = new Date();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
    System.out.println(simpleDateFormat.format(date));

「Asia/Calcutta」など、「America/New_York」に置き換えることができるさまざまなタイム ゾーンがあります。

于 2012-09-19T06:13:10.293 に答える
0

コード例を次に示します。サーバーのデフォルトのタイムゾーンをニューヨーク時間に明示的に設定していますが、Jon Lin のヒントに従って、自分のサーバーのデフォルトのタイムゾーンを特定することをお勧めします。たとえば、NY にいて、SF でホストされているサーバーを使用し、太平洋時間を使用している場合、どのゾーンでも予想される時間とは 3 時間の差が生じる可能性があります。

public void testTodayInIndia() {
    // For demonstration, make my system act as though it's in New York
    TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
    long oneDay = 86400000;
    long fourYears = (365 * 4 + 1) * oneDay;
    // Calculate 42 years after 1/1/1970 UTC
    Date now = new Date(fourYears * 10 + oneDay * 365 * 2 + 1);
    TimeZone calcutta = TimeZone.getTimeZone("Asia/Kolkata");
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // Since unspecified, this output will show the date and time in New York
    assertEquals("2011-12-31 19:00:00", formatter.format(now));
    // After setting the formatter's time zone, output reflects the same instant in Calcutta.
    formatter.setTimeZone(calcutta);
    assertEquals("2012-01-01 05:30:00", formatter.format(now));
}
于 2012-09-19T05:52:06.027 に答える