Time and time zone in Java

Source: Internet
Author: User
Tags time zones

For the Division of time zones, see Wikipedia: http://zh.wikipedia.org/wiki/%E6%99%82%E5%8D%80


In Java, the time class has two key classes:

Java. util. Calendar;

Java. util. Date;

When I was using the PostgreSQL database today, a timestamp with time zone was provided for the time type. I did not pay attention to the result. The time for writing data to the database is displayed on the page, but it does not reach the expected value, the problem is that the time format of this type is described in UTC. To correctly display the date of the local feature, additional processing is required.


I wrote a few tests to illustrate the time and time zone in Java.

1. Obtain the local Default Time Zone

@Test    public void test1() {        System.out.println("TimeZone.getDefault():" + TimeZone.getDefault());    }

Default Time Zone content:


TimeZone.getDefault():sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]

2. Supported Time Zone Information


@Test    public void test3() {        String[] ids = TimeZone.getAvailableIDs();        System.out.println("TimeZone AvailableIDs Numbers :" + ids.length);        for (String id : ids) {            System.out.println("TimeZone:Id-" + id);        }    }

Partial results:

TimeZone AvailableIDs Numbers :619TimeZone:Id-Etc/GMT+12TimeZone:Id-Etc/GMT+11TimeZone:Id-Pacific/MidwayTimeZone:Id-Pacific/NiueTimeZone:Id-Pacific/Pago_PagoTimeZone:Id-Pacific/SamoaTimeZone:Id-US/SamoaTimeZone:Id-America/AdakTimeZone:Id-America/AtkaTimeZone:Id-Etc/GMT+10TimeZone:Id-HSTTimeZone:Id-Pacific/HonoluluTimeZone:Id-Pacific/JohnstonTimeZone:Id-Pacific/RarotongaTimeZone:Id-Pacific/TahitiTimeZone:Id-SystemV/HST10TimeZone:Id-US/AleutianTimeZone:Id-US/Hawaii

3. Display GMT time and local time


@Test    public void test2() {        Date d = new Date();        System.out.println("Date.toGMTString():" + d.toGMTString());        System.out.println("Date.toLocaleString():" + d.toLocaleString());    }

Result:



Date.toGMTString():29 Sep 2013 10:25:37 GMTDate.toLocaleString():2013-9-29 18:25:37

4. The following is a comparison between the time method of the Date object and the Calendar Object method.

Public void test4 () {Date d = new Date (); System. out. println ("java. util. date: "); System. out. println ("Date. getYear (): "+ d. getYear (); System. out. println ("Date. getMonth (): "+ d. getMonth (); System. out. println ("Date. getDay (): "+ d. getDay (); System. out. println ("Date. getHours (): "+ d. getHours (); System. out. println ("Date. getMinutes (): "+ d. getMinutes (); System. out. println ("Date. getSeconds (): "+ d. getSeconds ();} public void test10 () {/*** after JDK1.1: discard the Date class and use the Calendar class */Calendar c = Calendar. getInstance (); System. out. println ("YEAR:" + (c. get (Calendar. YEAR); System. out. println ("MONTH:" + (c. get (Calendar. MONTH) + 1); System. out. println ("DAY_OF_MONTH:" + (c. get (Calendar. DAY_OF_MONTH); System. out. println ("HOUR_OF_DAY:" + (c. get (Calendar. HOUR_OF_DAY); System. out. println ("MINUTE:" + (c. get (Calendar. MINUTE); System. out. println ("SECOND:" + (c. get (Calendar. SECOND); System. out. println ("DAY_OF_WEEK:" + (c. get (Calendar. DAY_OF_WEEK); System. out. println ("DAY_OF_WEEK_IN_MONTH:" + (c. get (Calendar. DAY_OF_WEEK_IN_MONTH); System. out. println ("DAY_OF_YEAR:" + (c. get (Calendar. DAY_OF_YEAR )));}

Result:

Date class: Result java. util. date: Date. getYear (): 113Date. getMonth (): 8Date. getDay (): 0Date. getHours (): 18Date. getMinutes (): 25Date. getSeconds (): 37Calendar class: result YEAR: 2013 MONTH: 9DAY_OF_MONTH: 29HOUR_OF_DAY: 18 MINUTE: 25 SECOND: 37DAY_OF_WEEK: 1DAY_OF_WEEK_IN_MONTH: 5DAY_OF_YEAR: 272

Most methods of the Date class have been deprecated. We recommend that you use the Calendar class. The Calendar class provides more complete functions and powerful time processing methods and attributes.


4. About the number of years and milliseconds


The year obtained by the Date instance is the difference between the current year and 1900.

GetTime () of the Date instance returns the current time distance: 00:00:00 milliseconds.

Yuan00:00:00. 000, gregali calendar, January 1, January 1, 1970, and 00:00:00. 000 on January 1, January 1, 1900 are important references for date and time in Java development.


5. UTC time

@Test    public void test5() {        long times = Date.UTC(2, 2, 1, 1, 1, 59);        System.out.println("Date.UTC(2, 2, 1, 1, 1, 59):" + times);        times = Date.UTC(113, 2, 1, 1, 1, 59);        System.out.println("Date.UTC(113, 2, 1, 1, 1, 59):" + times);    }

Result:


Date.UTC(2, 2, 1, 1, 1, 59):-2140815481000Date.UTC(113, 2, 1, 1, 1, 59):1362099719000

The time reference point here is: 00:00:00. 000, January 1, January 1, 1970.


A Time Zone diagram is attached:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/143RR019-0.jpg "title =" time zone chart "alt =" 191120811.jpg"/>


This article is from the "wild horse red dust" blog and will not be reposted!

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.