最近接到一个需求:
给一个在美国洛杉矶时区(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值为1414918799000l,美国时间是2014-11-2 1:59:59;增加一秒1414918800000l,美国时间是2014-11-2 1:00:00,嘿嘿,是不是很神奇!!
不过仔细观察下你会发现,1414918799000l是夏令时PDT的 Sun Nov 02 01:59:59 PDT 2014;而1414918800000l是冬令时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
相关推荐
本文主要介绍了 GPS 自动调夏令时和冬令时授时系统的设计和实现,旨在解决广播发射台的手动调夏令时、冬令时的问题。该系统由 GPS 天线、GPS 接收机、授时电脑和发射机上位机组成。GPS 天线用于接收 GPS 卫星信号,...
夏令时(Daylight Saving Time, DST)是一个特殊的时间调整,某些地区在夏季会提前一个小时以利用更多日照时间。在`struct tm`中,`tm_isdst`字段用于标记是否处于夏令时期间。如果不确定当前时间是否为夏令时,该...
在计算机编程中,特别是使用JavaScript语言,判断当前时区是否实行夏令时是一项常见的需求,尤其对于处理时间数据的应用程序来说尤为重要。夏令时(Daylight Saving Time,简称DST)是一种为节约能源而人为调整本地...
在平面区域划分问题中,需要处理与图论相关的算法设计,而糖果公园问题则涉及图的遍历以及动态更新算法。这两个问题都需要参赛者具有良好的算法设计能力和编程实现能力。 在平面区域划分问题中,首先给出了一个平面...
在2015年的冬令营中,主办方精心准备了一系列丰富而深入的教学资料,以覆盖计算机科学与数学的多个重要主题。这些资料不仅涉及理论深度,还紧密联系实际应用,为学员提供了一个宝贵的学习机会,接下来,我们将逐一对...
通过这些讲义,参赛者能够在算法和数据结构方面得到全面的提升,从而在面对复杂问题时更加从容不迫。 练习题目是帮助参赛者巩固理论知识的有效方式。这些题目来源于历年竞赛中的真实问题,不仅具有针对性,还能帮助...
这份文件是《2021年思维挑战冬令营三年级试题及答案.pdf》,根据描述内容,我们可以将涉及的知识点进行归类和解读。以下是对这些题目的知识点分析: 1. 数列规律题:题中的“?”表示数列中缺失的部分,需要根据...
特别是二叉搜索树、平衡树(如AVL树和红黑树)和堆的数据结构,它们在解决复杂问题时能提供更高效的解决方案。 在算法竞赛中,时间复杂度和空间复杂度的分析也是关键内容。PPT可能详述如何分析算法的时间效率,如何...
【信息学竞赛】是指以计算机编程、算法设计和问题解决为核心的竞赛活动,主要针对中学生,旨在提升学生的逻辑思维、编程能力和创新意识。国家队专职教练的讲稿涉及到多个关键知识点和学习策略,对于参赛者来说具有...
2020年数学希望杯俱乐部五年级正式试题-冬令营,“希望杯”邀请赛自1990年以来,已经连续举行了二十八届。27年来,主办单位始终坚持比赛面向多数学校、多数学生,从命题、评奖到组织工作的每个环节,都围绕着一个...
《2003国家集训队冬令营试题与数据》是信息学竞赛领域的一份珍贵资源,尤其对于参与信息学奥林匹克(OI)比赛的学生和教练来说,具有极高的学习和参考价值。这份资料集包含了当年国家集训队冬令营的完整试题和相关...
这份资料是一份名为“2021 年思维挑战冬令营二年级试题及答案”的文件,其中包含了各种思维挑战题型,涵盖了逻辑推理、数学运算、图形识别等多个领域,旨在锻炼二年级学生的思维能力和问题解决能力。下面是根据给定...
根据提供的文件信息,该文件为《2021年清华大学文科营暨工科营(冬令营)完整试题及详细解析.pdf》,其内容涉及高中数学。由于文件内容未直接给出,因此无法提供具体试题和解析的内容,不过可以基于清华大学冬令营的...
2007全国青少年信息学奥林匹克竞赛冬令营 论文交流时间安排(每人12+3分钟) 1月28日 北京 高逸涵 与圆有关的离散化 四川2 王晓珂 解析一类组合游戏 湖南 仇荣琦 欧拉回路性质与应用探究 广东 余江伟 ...
在信息技术领域,尤其是算法设计与分析中,"圆方树"是一种高效的抽象数据结构,它在解决特定问题时展现出强大的能力。在2017年的中国计算机学会(CCF)冬季训练营中,讲师们特别强调了这一概念,并围绕其进行了深入...
在“2007冬令营-专题网络流算法”中,主要探讨了如何通过网络流模型来求解最大流问题。 首先,一个网络流图G=(V, E, C)是由顶点集合V、边集合E和每条边的容量C组成,其中S是源点,T是汇点,其他顶点是中转站。每条...
3. **编程技巧**:参赛者会分享他们在编写代码时的技巧,如快速输入输出、错误处理、调试技巧、代码优化等,这些都是提高程序效率和正确性的关键。 4. **团队合作**:ACM竞赛强调团队合作,论文可能会涉及如何分配...
这意味着在解决这类问题时,我们可以通过找到最佳匹配来达到最优解。 König定理的证明通常基于构造性方法,例如,可以从一个最小覆盖构造出一个同样大小的匹配,或者反之。这种转化能力使得我们可以灵活地在匹配和...
学生们在解决代数问题时,不仅要掌握基本的运算规律,还要能够灵活运用这些规律解决实际问题,如数列规律和方程求解。方程求解往往需要学生具有良好的代数基础和逻辑推理能力,以及对未知数求解过程的清晰理解。几何...
冬令营.mmap