yyyy-MM-dd HH:mmという形式の文字列が、UTCで表されます。 次のステップは、これをカレンダー(TimeZone UTC)に入れることです。Javaカレンダーと時刻UTCからBSTへ
さらに、UTCを「ヨーロッパ/ロンドン」のtimeZone(GMT/BST)に変換した別のカレンダーを作成する必要があります。
その後、「ヨーロッパ/ロンドン」カレンダーにDST(昼光節約オフセット)があるかどうかを検出できる必要があります。
以下のコードは、私がどれだけ持っているかを示し、デフォルトのシステムタイムゾーンがGMTのイギリスのコンピュータで正常に動作します。しかし、タイムゾーンがUTCのPCで実行すると失敗します。 DST_offset(常にゼロ)があるかどうかは私には分かりません。
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.set(Calendar.YEAR, 2016);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 27);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 35);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
//This is required as Java Date JAN starts at 0.
int MonthCon = cal.get(Calendar.MONTH)-1;
cal.set(Calendar.MONTH, MonthCon);
Date d = cal.getTime();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssz");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("UTC: " + f.format(d));
f.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println("BST: " + f.format(d));
//Creates a BST calendar of the same UTC time
String dateStrBST = f.format(d);
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssz");
curFormater.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date dateObjBST = curFormater.parse(dateStrBST);
System.out.println("BSTNewDate: " + f.format(dateObjBST));
Calendar calBST = Calendar.getInstance(TimeZone.getTimeZone("BST"));
calBST.setTime(dateObjBST);
System.out.println("Current TimeZone is : " + calBST.getTimeZone());
int offset = calBST.get(Calendar.DST_OFFSET);
System.out.println("Day Light Savings: "+offset);
System.out.println("Transition Day: "+isDSTTransitionDay(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH))+" Transition Type: "+DSTtransitionType(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH)));
残念ながら、私は任意の特定の日が移行日であるかどうかを検出できるようにする必要があり、それはオン/オフまたはオン/オフDSTから変更日を言うことです。これもローカルコンピュータでは動作しますが、UTCのタイムゾーンでは動作しません。
private static boolean isDSTTransitionDay(int year, int month, int day) throws ParseException
{
Calendar calStartofDay = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calStartofDay.set(Calendar.YEAR, year);
calStartofDay.set(Calendar.MONTH, month);
calStartofDay.set(Calendar.DAY_OF_MONTH, day);
calStartofDay.set(Calendar.HOUR_OF_DAY, 00);
calStartofDay.set(Calendar.MINUTE, 0);
calStartofDay.set(Calendar.SECOND, 1);
Date dStartofDay = calStartofDay.getTime();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssz");
f.setTimeZone(TimeZone.getTimeZone("Europe/London"));
String dateStrUTCtoBST = f.format(dStartofDay);
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssz");
curFormater.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date dateObjBST = curFormater.parse(dateStrUTCtoBST);
Calendar calBST = Calendar.getInstance();
calBST.setTime(dateObjBST);
int offsetStart = calBST.get(Calendar.DST_OFFSET);
calBST.add(Calendar.HOUR_OF_DAY, 23);
int offsetEnd = calBST.get(Calendar.DST_OFFSET);
//System.out.println("Start: "+offsetStart+" End: "+offsetEnd);
if (offsetEnd == offsetStart)
{
return false;
}else
{
//if(offsetStart<offsetEnd) {System.out.println("Transition to BST");}else{System.out.println("Transition to UTC/GMT");};
return true;
}
}
したがって、UTCコンピュータでは、常にCalendar.DST_OFFSETを0に設定するため、悲惨な結果に終わります。私は途中で何かを間違いなく誤解していますので、どんな助けや明確さもいいと思います。
私は、カレンダーの残りの部分を使っているので、かなりカレンダーを使い続ける必要がありますが、Java8にはより多くの工夫が施されています。
AFAIK、UTCにはDSTはありません。 GMTはUTCと等しくありません。 – SlumpA
https://www.timeanddate.com/time/zone/uk/london – DevilCode
「UTCコンピュータは常にCalendar.DST_OFFSETをゼロにしてしまうので、悲惨なことに失敗します」というのは、その定義が常にゼロのオフセットになるはずです。あなたはそれが何をすると思いますか?フランス語でBTW、UTCはロンドンではなくパリで1で定義されます。 –