フォーマットされた日付文字列を日付オブジェクトに変換しようとしています。日付文字列が他のタイムゾーンにフォーマットされています。
私が行うsdf.parse(String)
と、システム日付オブジェクトが返されます。
コードは以下の通り、
static Date convertGMTTime(String timeZone, long longDate){
Date convertedTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date date = new Date(longDate);
System.out.println("timezone: "+timeZone +", timestamp: "+date);
Locale locale = Locale.ENGLISH;
TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
System.out.println("Source timezone: "+destTimeZone);
/* DateFormat formatter = DateFormat.getDateTimeInstance(
DateFormat.DEFAULT,
DateFormat.DEFAULT,
locale);
formatter.setTimeZone(destTimeZone);*/
sdf.setTimeZone(destTimeZone);
String convertedDateStr = sdf.format(date);
System.out.println("convertedDateStr: "+convertedDateStr);
convertedTime = sdf.parse(convertedDateStr);
System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
}catch(Exception e){
e.printStackTrace();
}
return convertedTime;
}
誰かが助けて、私が間違っているところを指摘してくれれば幸いです。前もって感謝します。
出力:
タイムゾーン: Atlantic/Cape_Verde、タイムスタンプ: Tue Jun 26 17:38:11 IST 2012
ソースタイムゾーン: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,トランジション=6,lastRule=null]変換されたDateStr: 2012-06-26 11:08:11
変換時間: 火曜日 6 月 26 日 17:38:11 IST 2012
sdf:sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null ]
共有する詳細がいくつかあります。別の sdf オブジェクトを (タイムゾーンを設定せずに) 使用すると、正しい時刻と日付が返されますが、タイムゾーンはシステムクロックから選択されます
コード
static Date convertGMTTime(String timeZone, long longDate){
Date convertedTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdfParse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date date = new Date(longDate);
TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
System.out.println("Source timezone: "+destTimeZone);
sdf.setTimeZone(destTimeZone);
String convertedDateStr = sdf.format(date);
System.out.println("convertedDateStr: "+convertedDateStr );
convertedTime = sdfParse.parse(convertedDateStr,new ParsePosition(0));
System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
}catch(Exception e){
e.printStackTrace();
}
return convertedTime;
}
出力
Source timezone: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
convertedDateStr: 2012-06-26 12:24:56
convertedTime: Tue Jun 26 12:24:56 IST 2012
sdf: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
タイムゾーンを sdf に割り当てないとシステム タイム ゾーンが使用されることは理解していますが、システム タイム ゾーンで時間が表示されないのはなぜですか? 文字列と同じようにタイムゾーンで表示しますが、タイムゾーンが異なります。
回答タイムゾーンを設定すると、sdfに他のタイムゾーンが設定されているという事実に関係なく、システム時間に従って日付オブジェクトが返されます。
sdf.parse と sdf.format の機能的な動作を説明してください。
私にとって、sdf.setTimeZone() は format を使用すると影響を与え、sdf.parse() を使用すると無効になります。かなり奇妙だと思います。
この点で助けに感謝します。