JAva夏時間、冬時間の問題の処理
16381 ワード
最近需要がありました.
アメリカのロサンゼルスタイムゾーン(America/LOs_Angeles)のマシンで生成されたlongの時間を、中国タイムゾーンのマシンで、この時間をアメリカ時間に変換しますか?
業務側は、特に主な夏時間、冬時間の問題が必要だと注意した.
そこで夏時間、冬時間の問題を検討しました.
1、まずこの問題を検索して、javaの中ですでにこの問題を自分で処理したことを知っていて、Javaの中で毎日標準的な24時間ではありませんて、23かもしれなくて、25かもしれません.23時間と25時間は夏、冬の時に起こります.
出力:
中国のタイムゾーンは1992年以降に夏時間、冬時間が廃止された場合、GMTタイムゾーンは存在しないことがわかります
サマータイム
アメリカのロサンゼルスでは
サマータイム.
2、一日でどうして23、25時間になるのか知りたいので、試験を続けます.
出力:
long値は14144918799000 lであり、米国時間は2014-11-21:59:59であることが分かった.1秒1414918800000 l増加して、アメリカ時間は2014-11-21:00:00で、へへへ、不思議ではありませんか?!
よく見ると14149,1879,900 lはサマータイムPDTです Sun Nov 02 01:59:59 PDT 2014;1414918800000 lは冬のPSTの Sun Nov 02 01:00:00 PST 2014.
これで問題がわかった!!
3、需要に戻る
Dateがタイムゾーンと関係があると考えると、変換前にタイムゾーンを設定すれば問題は解決します.
over
アメリカのロサンゼルスタイムゾーン(America/LOs_Angeles)のマシンで生成されたlongの時間を、中国タイムゾーンのマシンで、この時間をアメリカ時間に変換しますか?
業務側は、特に主な夏時間、冬時間の問題が必要だと注意した.
そこで夏時間、冬時間の問題を検討しました.
1、まずこの問題を検索して、javaの中ですでにこの問題を自分で処理したことを知っていて、Javaの中で毎日標準的な24時間ではありませんて、23かもしれなくて、25かもしれません.23時間と25時間は夏、冬の時に起こります.
package com.mike.test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class WhatTime {
/**
* Dates those have not EXACTLY 24 hours ?
**/
public static void testDayTime(TimeZone timeZone) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Time Zone is " + timeZone.getDisplayName() + " " + timeZone.getID());
Calendar start = Calendar.getInstance(timeZone);
start.setTime(new Date(0));// UTC 1970-01-01
System.out.println("start=" + fmt.format(start.getTime()));
long now = Calendar.getInstance(timeZone).getTimeInMillis();
long year = 1000l * 3600 * 24 * 365;
long end = now + year * 5;//
// System.out.println("now=" + now + "\tend" + end);
System.out.println(new Date(end));
// time
boolean find = false;
for (long i = start.getTimeInMillis(); i < end; i = start.getTimeInMillis()) {
start.add(Calendar.DATE, 1); // add one day
if ((start.getTimeInMillis() - i) % (24 * 3600 * 1000L) != 0) {
find = true;
System.out.println("from " + fmt.format(new Date(i)) + "to " + fmt.format(start.getTime()) + " has "
+ (start.getTimeInMillis() - i) + "ms" + "[" + (start.getTimeInMillis() - i) / (3600 * 1000L)
+ "hours]");
}
}
if (!find) {
System.out.println("Every day is ok.");
}
}
public static void main(String argv[]) throws Exception {
TimeZone timeZone = TimeZone.getDefault();
WhatTime.testDayTime(timeZone);
System.out.println("----------------------------------------------------------------");
timeZone = TimeZone.getTimeZone("GMT");
WhatTime.testDayTime(timeZone);
System.out.println("----------------------------------------------------------------");
timeZone = TimeZone.getTimeZone("America/Los_Angeles");
WhatTime.testDayTime(timeZone);
}
}
出力:
Time Zone is Asia/Shanghai
start=1970-01-01 08:00:00
Mon Sep 02 20:46:37 CST 2019
from 1986-05-03 08:00:00to 1986-05-04 08:00:00 has 82800000ms[23hours]
from 1986-09-13 08:00:00to 1986-09-14 08:00:00 has 90000000ms[25hours]
from 1987-04-11 08:00:00to 1987-04-12 08:00:00 has 82800000ms[23hours]
from 1987-09-12 08:00:00to 1987-09-13 08:00:00 has 90000000ms[25hours]
from 1988-04-09 08:00:00to 1988-04-10 08:00:00 has 82800000ms[23hours]
from 1988-09-10 08:00:00to 1988-09-11 08:00:00 has 90000000ms[25hours]
from 1989-04-15 08:00:00to 1989-04-16 08:00:00 has 82800000ms[23hours]
from 1989-09-16 08:00:00to 1989-09-17 08:00:00 has 90000000ms[25hours]
from 1990-04-14 08:00:00to 1990-04-15 08:00:00 has 82800000ms[23hours]
from 1990-09-15 08:00:00to 1990-09-16 08:00:00 has 90000000ms[25hours]
from 1991-04-13 08:00:00to 1991-04-14 08:00:00 has 82800000ms[23hours]
from 1991-09-14 08:00:00to 1991-09-15 08:00:00 has 90000000ms[25hours]
----------------------------------------------------------------
Time Zone is GMT
start=1970-01-01 08:00:00
Mon Sep 02 20:46:37 CST 2019
Every day is ok.
----------------------------------------------------------------
Time Zone is America/Los_Angeles
start=1970-01-01 08:00:00
Mon Sep 02 20:46:37 CST 2019
from 1970-04-26 08:00:00to 1970-04-27 07:00:00 has 82800000ms[23hours]
from 1970-10-25 07:00:00to 1970-10-26 08:00:00 has 90000000ms[25hours]
from 1971-04-25 08:00:00to 1971-04-26 07:00:00 has 82800000ms[23hours]
from 1971-10-31 07:00:00to 1971-11-01 08:00:00 has 90000000ms[25hours]
from 1972-04-30 08:00:00to 1972-05-01 07:00:00 has 82800000ms[23hours]
from 1972-10-29 07:00:00to 1972-10-30 08:00:00 has 90000000ms[25hours]
from 1973-04-29 08:00:00to 1973-04-30 07:00:00 has 82800000ms[23hours]
from 1973-10-28 07:00:00to 1973-10-29 08:00:00 has 90000000ms[25hours]
from 1974-01-06 08:00:00to 1974-01-07 07:00:00 has 82800000ms[23hours]
from 1974-10-27 07:00:00to 1974-10-28 08:00:00 has 90000000ms[25hours]
from 1975-02-23 08:00:00to 1975-02-24 07:00:00 has 82800000ms[23hours]
from 1975-10-26 07:00:00to 1975-10-27 08:00:00 has 90000000ms[25hours]
from 1976-04-25 08:00:00to 1976-04-26 07:00:00 has 82800000ms[23hours]
from 1976-10-31 07:00:00to 1976-11-01 08:00:00 has 90000000ms[25hours]
from 1977-04-24 08:00:00to 1977-04-25 07:00:00 has 82800000ms[23hours]
from 1977-10-30 07:00:00to 1977-10-31 08:00:00 has 90000000ms[25hours]
from 1978-04-30 08:00:00to 1978-05-01 07:00:00 has 82800000ms[23hours]
from 1978-10-29 07:00:00to 1978-10-30 08:00:00 has 90000000ms[25hours]
from 1979-04-29 08:00:00to 1979-04-30 07:00:00 has 82800000ms[23hours]
from 1979-10-28 07:00:00to 1979-10-29 08:00:00 has 90000000ms[25hours]
from 1980-04-27 08:00:00to 1980-04-28 07:00:00 has 82800000ms[23hours]
from 1980-10-26 07:00:00to 1980-10-27 08:00:00 has 90000000ms[25hours]
from 1981-04-26 08:00:00to 1981-04-27 07:00:00 has 82800000ms[23hours]
from 1981-10-25 07:00:00to 1981-10-26 08:00:00 has 90000000ms[25hours]
from 1982-04-25 08:00:00to 1982-04-26 07:00:00 has 82800000ms[23hours]
from 1982-10-31 07:00:00to 1982-11-01 08:00:00 has 90000000ms[25hours]
from 1983-04-24 08:00:00to 1983-04-25 07:00:00 has 82800000ms[23hours]
from 1983-10-30 07:00:00to 1983-10-31 08:00:00 has 90000000ms[25hours]
from 1984-04-29 08:00:00to 1984-04-30 07:00:00 has 82800000ms[23hours]
from 1984-10-28 07:00:00to 1984-10-29 08:00:00 has 90000000ms[25hours]
from 1985-04-28 08:00:00to 1985-04-29 07:00:00 has 82800000ms[23hours]
from 1985-10-27 07:00:00to 1985-10-28 08:00:00 has 90000000ms[25hours]
from 1986-04-27 08:00:00to 1986-04-28 07:00:00 has 82800000ms[23hours]
from 1986-10-26 07:00:00to 1986-10-27 08:00:00 has 90000000ms[25hours]
from 1987-04-05 08:00:00to 1987-04-06 07:00:00 has 82800000ms[23hours]
from 1987-10-25 07:00:00to 1987-10-26 08:00:00 has 90000000ms[25hours]
from 1988-04-03 08:00:00to 1988-04-04 07:00:00 has 82800000ms[23hours]
from 1988-10-30 07:00:00to 1988-10-31 08:00:00 has 90000000ms[25hours]
from 1989-04-02 08:00:00to 1989-04-03 07:00:00 has 82800000ms[23hours]
from 1989-10-29 07:00:00to 1989-10-30 08:00:00 has 90000000ms[25hours]
from 1990-04-01 08:00:00to 1990-04-02 07:00:00 has 82800000ms[23hours]
from 1990-10-28 07:00:00to 1990-10-29 08:00:00 has 90000000ms[25hours]
from 1991-04-07 08:00:00to 1991-04-08 07:00:00 has 82800000ms[23hours]
from 1991-10-27 07:00:00to 1991-10-28 08:00:00 has 90000000ms[25hours]
from 1992-04-05 08:00:00to 1992-04-06 07:00:00 has 82800000ms[23hours]
from 1992-10-25 07:00:00to 1992-10-26 08:00:00 has 90000000ms[25hours]
from 1993-04-04 08:00:00to 1993-04-05 07:00:00 has 82800000ms[23hours]
from 1993-10-31 07:00:00to 1993-11-01 08:00:00 has 90000000ms[25hours]
from 1994-04-03 08:00:00to 1994-04-04 07:00:00 has 82800000ms[23hours]
from 1994-10-30 07:00:00to 1994-10-31 08:00:00 has 90000000ms[25hours]
from 1995-04-02 08:00:00to 1995-04-03 07:00:00 has 82800000ms[23hours]
from 1995-10-29 07:00:00to 1995-10-30 08:00:00 has 90000000ms[25hours]
from 1996-04-07 08:00:00to 1996-04-08 07:00:00 has 82800000ms[23hours]
from 1996-10-27 07:00:00to 1996-10-28 08:00:00 has 90000000ms[25hours]
from 1997-04-06 08:00:00to 1997-04-07 07:00:00 has 82800000ms[23hours]
from 1997-10-26 07:00:00to 1997-10-27 08:00:00 has 90000000ms[25hours]
from 1998-04-05 08:00:00to 1998-04-06 07:00:00 has 82800000ms[23hours]
from 1998-10-25 07:00:00to 1998-10-26 08:00:00 has 90000000ms[25hours]
from 1999-04-04 08:00:00to 1999-04-05 07:00:00 has 82800000ms[23hours]
from 1999-10-31 07:00:00to 1999-11-01 08:00:00 has 90000000ms[25hours]
from 2000-04-02 08:00:00to 2000-04-03 07:00:00 has 82800000ms[23hours]
from 2000-10-29 07:00:00to 2000-10-30 08:00:00 has 90000000ms[25hours]
from 2001-04-01 08:00:00to 2001-04-02 07:00:00 has 82800000ms[23hours]
from 2001-10-28 07:00:00to 2001-10-29 08:00:00 has 90000000ms[25hours]
from 2002-04-07 08:00:00to 2002-04-08 07:00:00 has 82800000ms[23hours]
from 2002-10-27 07:00:00to 2002-10-28 08:00:00 has 90000000ms[25hours]
from 2003-04-06 08:00:00to 2003-04-07 07:00:00 has 82800000ms[23hours]
from 2003-10-26 07:00:00to 2003-10-27 08:00:00 has 90000000ms[25hours]
from 2004-04-04 08:00:00to 2004-04-05 07:00:00 has 82800000ms[23hours]
from 2004-10-31 07:00:00to 2004-11-01 08:00:00 has 90000000ms[25hours]
from 2005-04-03 08:00:00to 2005-04-04 07:00:00 has 82800000ms[23hours]
from 2005-10-30 07:00:00to 2005-10-31 08:00:00 has 90000000ms[25hours]
from 2006-04-02 08:00:00to 2006-04-03 07:00:00 has 82800000ms[23hours]
from 2006-10-29 07:00:00to 2006-10-30 08:00:00 has 90000000ms[25hours]
from 2007-03-11 08:00:00to 2007-03-12 07:00:00 has 82800000ms[23hours]
from 2007-11-04 07:00:00to 2007-11-05 08:00:00 has 90000000ms[25hours]
from 2008-03-09 08:00:00to 2008-03-10 07:00:00 has 82800000ms[23hours]
from 2008-11-02 07:00:00to 2008-11-03 08:00:00 has 90000000ms[25hours]
from 2009-03-08 08:00:00to 2009-03-09 07:00:00 has 82800000ms[23hours]
from 2009-11-01 07:00:00to 2009-11-02 08:00:00 has 90000000ms[25hours]
from 2010-03-14 08:00:00to 2010-03-15 07:00:00 has 82800000ms[23hours]
from 2010-11-07 07:00:00to 2010-11-08 08:00:00 has 90000000ms[25hours]
from 2011-03-13 08:00:00to 2011-03-14 07:00:00 has 82800000ms[23hours]
from 2011-11-06 07:00:00to 2011-11-07 08:00:00 has 90000000ms[25hours]
from 2012-03-11 08:00:00to 2012-03-12 07:00:00 has 82800000ms[23hours]
from 2012-11-04 07:00:00to 2012-11-05 08:00:00 has 90000000ms[25hours]
from 2013-03-10 08:00:00to 2013-03-11 07:00:00 has 82800000ms[23hours]
from 2013-11-03 07:00:00to 2013-11-04 08:00:00 has 90000000ms[25hours]
from 2014-03-09 08:00:00to 2014-03-10 07:00:00 has 82800000ms[23hours]
from 2014-11-02 07:00:00to 2014-11-03 08:00:00 has 90000000ms[25hours]
from 2015-03-08 08:00:00to 2015-03-09 07:00:00 has 82800000ms[23hours]
from 2015-11-01 07:00:00to 2015-11-02 08:00:00 has 90000000ms[25hours]
from 2016-03-13 08:00:00to 2016-03-14 07:00:00 has 82800000ms[23hours]
from 2016-11-06 07:00:00to 2016-11-07 08:00:00 has 90000000ms[25hours]
from 2017-03-12 08:00:00to 2017-03-13 07:00:00 has 82800000ms[23hours]
from 2017-11-05 07:00:00to 2017-11-06 08:00:00 has 90000000ms[25hours]
from 2018-03-11 08:00:00to 2018-03-12 07:00:00 has 82800000ms[23hours]
from 2018-11-04 07:00:00to 2018-11-05 08:00:00 has 90000000ms[25hours]
from 2019-03-10 08:00:00to 2019-03-11 07:00:00 has 82800000ms[23hours]
中国のタイムゾーンは1992年以降に夏時間、冬時間が廃止された場合、GMTタイムゾーンは存在しないことがわかります
サマータイム
アメリカのロサンゼルスでは
サマータイム.
2、一日でどうして23、25時間になるのか知りたいので、試験を続けます.
package com.mike.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class GetAmericaLosTime {
private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
//
// // 15
// String dateString = "2014-08-31 07:00:00";
//
// Date summer = fmt.parse(dateString);
// printDate(summer.getTime());
//
// // 16
// dateString = "2014-12-02 07:00:00";
// Date winter = fmt.parse(dateString);
// printDate(winter.getTime());
// 1
long abc = 1414918799000l;
printDate(abc);
abc = 1414918800000l;
printDate(abc);
// 2
// long bcd = 1394359199000l;
// printDate(bcd);
// bcd = 1394359200000l;
// printDate(bcd);
// printSameTime();
// : long : 1414918799000l :2014-11-2 16:59:59
// return new Date(time).toLocaleString(), 2014-11-2 1:59:59
// return new Date(time).toString(); ,Sun Nov 02 01:59:59 PDT
// 2014
}
private static void printDate(long now) {
//
// TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
// System.out.println(new Date(now).toString());
// System.out.println(new Date(now).toLocaleString());
//
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println(new Date(now));
System.out.println(new Date(now).toLocaleString());
System.out.println("============================================================");
}
private static String conventTime(long time) {
//
// TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
//
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
return new Date(time).toLocaleString();
}
private static void printSameTime() throws ParseException {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
// long, date
String dateString = "2014-03-10 00:00:00";
Date special = fmt.parse(dateString);
long specialTime = special.getTime();
System.out.println(specialTime);
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
long oneHour = 3600 * 1000l;
for (int i = -26; i < 26; i++) {
System.out.println("date long:" + (specialTime + oneHour * i) + " \tdate"
+ new Date(specialTime + oneHour * i));
}
}
private static void printTestDate() {
long start = System.currentTimeMillis();
System.out.println("start " + new Date(start));
System.out.println("start+one hour " + new Date(start + 3600 * 1000));
System.out.println("start+one day " + new Date(start + 3600 * 1000 * 24));
System.out.println("start+one year " + new Date(start + 3600l * 1000 * 24 * 365));
System.out.println("start+ten year " + new Date(start + 3600l * 1000 * 24 * 365 * 10));
System.out.println("now " + new Date(0));
}
}
出力:
Sun Nov 02 01:59:59 PDT 2014
2014-11-2 1:59:59
============================================================
Sun Nov 02 01:00:00 PST 2014
2014-11-2 1:00:00
============================================================
long値は14144918799000 lであり、米国時間は2014-11-21:59:59であることが分かった.1秒1414918800000 l増加して、アメリカ時間は2014-11-21:00:00で、へへへ、不思議ではありませんか?!
よく見ると14149,1879,900 lはサマータイムPDTです Sun Nov 02 01:59:59 PDT 2014;1414918800000 lは冬のPSTの Sun Nov 02 01:00:00 PST 2014.
これで問題がわかった!!
3、需要に戻る
Dateがタイムゾーンと関係があると考えると、変換前にタイムゾーンを設定すれば問題は解決します.
private static String conventTime(long time) {
//
// TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
//
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
return new Date(time).toLocaleString();
}
over