0
タイムゾーンの入力日時と入力日時のタイムゾーンを指定し、関連する日時を予想されるタイムゾーンに設定します。Javaの特定の日時のタイムゾーン変換
これは私の方法です。 convertToTimezone( "03/08/2010 20:19:00 PM"、 "アジア/上海"、 "米国/中部"); 上記の時間は、アジア/上海での時間です。US/Centralでの対応時間は何ですか?
実際にはうまくいきますが、実際の時間と1時間差があります。 どこが間違っているのか分かりますか?
ここにコードがあります。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class DateUtil
{
private static String format_date="MM/dd/yyyy HH:mm:ss a";
public static void main(String a[])
{
try
{
String sourceTimezone="Asia/Shanghai";
String destTimezone="US/Central";
String outputExpectedTimezone=convertToTimezone("03/08/2010 20:19:00 PM",sourceTimezone,destTimezone);
System.out.println("outputExpectedTimezone :"+outputExpectedTimezone);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static String convertToTimezone(String inputDate,String inputDateTimezone,String destinationDateTimezone)throws Exception
{
String outputDate=null;
SimpleDateFormat format = new SimpleDateFormat(format_date);
format.setTimeZone(TimeZone.getTimeZone(inputDateTimezone));
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(inputDateTimezone));
calendar.setTime(format.parse(inputDate));
calendar.add(Calendar.MILLISECOND,-(calendar.getTimeZone().getRawOffset()));
calendar.add(Calendar.MILLISECOND, - calendar.getTimeZone().getDSTSavings());
calendar.add(Calendar.MILLISECOND, TimeZone.getTimeZone(destinationDateTimezone).getRawOffset());
outputDate=format.format(calendar.getTime());
return outputDate;
}
}