About TimeZone timeout in Java, timezone timeout

Source: Internet
Author: User
Tags time zones

About TimeZone timeout in Java, timezone timeout

Use the system-config-date tool on Linux to set a TimeZone (the setting result will be recorded in the/etc/sysconfig/clock file ), the time obtained in Java cannot change with the clock at the time of seeking, which leads to chaotic system time in Java.

We have written a small program to get the TimeZone information on the current machine, and print some time to see the time changes when the inbound/outbound traffic changes.

import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.TimeZone; public class TimeZoneTest {    public static void main(String[] args) {        TimeZone tz = TimeZone.getDefault();        System.out.println("tz: " + tz);                 int offset = tz.getRawOffset();        System.out.println("raw offset: " + offset);                 int dstSavings = tz.getDSTSavings();        System.out.println("dstSavings: " + dstSavings);                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        while(true) {            Calendar cal = Calendar.getInstance();            String msg = "[" + sdf.format(cal.getTime()) + "] " + cal.getTime();            msg += ", offset: " + TimeZone.getDefault().getOffset(cal.getTimeInMillis());            System.out.println(msg);                         try {                Thread.sleep(60 * 1000);            } catch (InterruptedException ex) {                ex.printStackTrace();            }        }    }}

The following two correct cases are the running results when the TimeZone is set to "America/Los_Angeles ".

Case 1: set the system time to 01:59:00 AM

tz: sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]raw offset: -28800000dstSavings: 3600000[2014-03-09 01:59:03] Sun Mar 09 01:59:03 PST 2014, offset: -28800000[2014-03-09 03:00:03] Sun Mar 09 03:00:03 PDT 2014, offset: -25200000[2014-03-09 03:01:03] Sun Mar 09 03:01:03 PDT 2014, offset: -25200000

Case 2: set the system time to 12:59:00 AM

tz: sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]raw offset: -28800000dstSavings: 3600000[2014-11-02 01:59:32] Sun Nov 02 01:59:32 PDT 2014, offset: -25200000[2014-11-02 01:00:32] Sun Nov 02 01:00:32 PST 2014, offset: -28800000[2014-11-02 01:01:32] Sun Nov 02 01:01:32 PST 2014, offset: -28800000

Case 3: Set TimeZone to "America/North_Dakota/Center", set the system time to 01:59:00 AM, and then run the above program to find

tz: sun.util.calendar.ZoneInfo[id="GMT-06:00",offset=-21600000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]raw offset: -21600000dstSavings: 0[2014-03-09 01:59:12] Sun Mar 09 01:59:12 GMT-06:00 2014, offset: -21600000, loader start date: Sun Mar 09 01:59:00 GMT-06:00 2014[2014-03-09 02:00:12] Sun Mar 09 02:00:12 GMT-06:00 2014, offset: -21600000, loader start date: Sun Mar 09 02:00:00 GMT-06:00 2014[2014-03-09 02:01:12] Sun Mar 09 02:01:12 GMT-06:00 2014, offset: -21600000, loader start date: Sun Mar 09 02:01:00 GMT-06:00 2014

From the results, we can see that TimeZone ID = GMT-06: 00, dstSavings = 0, and the time has not been adjusted backward to when the time has changed from, this indicates that Java does not find the corresponding time zone information (maybe this is a bug in Java), so Java does not know whether the current time zone uses the timeout, so the obtained time is incorrect.

You can solve these problems in the following two ways:

1. Use TZ environment variables and then run the Java program

$ export TZ=America/North_Dakota/Center$ java TimeZoneTest

2. Use-Duser. timezone = America/North_Dakota/Center as the system parameter of the Java Virtual Machine.

java -Duser.timezone=America/North_Dakota/Center TimeZoneTest

In addition, you can use the following commands to view the timezone information of the system.

Cat/usr/share/zoneinfo/zone. tabzdump/usr/share/zoneinfo/* zdump-v/usr/share/zoneinfo/America/New_York view zdump-v/etc/localtimetz_convert/usr/share/zoneinfo /sudo cat/etc/sysconfig/clock

Java, who has the TimeZone ID of each country?

You can check the connection below.

NZDT + New Zealand Daylight Time System
IDLE + international date change line, East
NZST + New Zealand Standard Time
NZT + New Zealand Time
AESST + standard Daylight Time for eastern Australia
ACSST + Standard Australian Daylight Time System
CADT + middle Australian Daylight Time System
SADT + South Australia Daylight Saving Time
AEST + Standard Time for eastern Australia
EAST + eastern Australia Standard Time
GST + Guam standard time, Russian Time Zone 9
LIGT + Melbourne, Australia
SAST + South Australia Standard Time
Standard Australian time in CAST +
AWSST + standard Daylight Time for Western Australia
JST + standard Japanese time (Russian Time Zone 8)
KST + standard Korean time
MHT + 09:00 Kwajalein Time
WDT + West Australia Daylight Time System
MT + 08:30 Mauritius (moluccas) Time (?)
AWST + Standard Time for Western Australia
CCT + China Coastal time
WADT + West Australia Daylight Time System
WST + Standard Time for West Australia
JT + 07:30 Java time
ALMST + Almaty hour
WAST + Standard Time for West Australia
CXT + 07:00 Christmas (Easter ?) Island time
MT + Myannar time
ALMT + 06:00 Almaty time
MAWT + 06:00 Mawson (Antarctica) Time
IOT + India Chagos time
MVT + 05:00 Maldives (?) Island time
TFT + 05:00 Kerguelen time
AFT + Afghanistan time
EAST + Antananarivo Summer Time
MUT + Mauritius Island Time
RET + Reunion Island Time
SCT + Mahe Island Time
IRT, IT + Iran time
EAT + 0: 00 Antananarivo, Comoro Time
BT + Baghdad time
EETDST + Eastern Europe Daylight Time System
HMT + 0:00 Hellas Mediterranean Time (?)
BDST + British ish Double Standard Time
CEST + Central Europe hour
CETDST + Central European Daylight Time System
EET + Eastern Europe, (Russian Time Zone 1)
FWT + French winter time
IST + Israel Standard Time
MEST + Central Europe Daylight Time System
METDST + Central Europe Daylight Time
SST + Swedish Daylight Time System
BST + UK Daylight Saving Time
CET + Central Europe time
DNT + 0: 00 Dansk Normal Tid
FS ...... remaining full text>

New date () in java ()

Your problem is related to the system JVM. You can set the time zone and use the following statement:

Import java. util .*;

TimeZone tz = TimeZone. getTimeZone ("ETC/GMT-8 ");
TimeZone. setDefault (tz );

// Display all correct time zones in the Java System
String [] ids = TimeZone. getAvailableIDs ();
For (int I = 0; I <ids. length; I ++)
System. out. println (ids [I]. toString ());

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.