The time zone conversion java

The time zone conversion java

First, the description of the time zone

Earth's surface by the warp threads from east to west, is designated as one region, a predetermined area adjacent time difference of 1 hour. The easternmost and westernmost of people in the same region of the difference in time to see the sun rise at most 1 hour. When people across a region, will own clock correction 1 hour (one hour minus west, east plus 1 hour), across the area to plus or minus a few hours, the same time in different time zones indicated time are different.

Second, the representation of a time

When a string is usually formatted We usually express to represent a time, such as "2019-11-5 20:05" this string representation is at 20:05 on November 5th 2019. But there is an implicit premise that the time zone is the time zone of your current location, the East eight districts of people see this time is considered to 20:05 East eight districts, nine districts of the East who would think that nine districts of the East 20:05, in fact, the two time difference of one hour. So to the right represents a particular moment, but also coupled with the time zone information, such as "2019-11-5 20:05 +8: 00" This man no matter which time zone string see knows represents the East eight districts time.

Third, the time stamp

Timestamp represents the time interval from 0 zone view, expressed from time to time "1970-1-1 00:00 +0:: 00" up to now (002019-11-5 12:05 +0) spacing, and represents District 8 from the point of view from the East "1970-1-1 08:00 +8: 00" up to now (2019-11-5 20:05 +8: 00) time intervals.

Interval = now - the starting point, because now both the starting point and a difference of eight hours, the time interval is the same, the same token, in any time zone in the time stamp now this moment are the same.

Four, Date and time stamp class

Date of java class is actually saved time stamp a certain moment, it is the same time in any time zone to get the Date object are the same. When using SimpleDateFormat date format, the default will be the current system time zone to format dates, so the result is not the same time zone while people will have to format dates to get the time difference.

So the following code in the East eight districts and nine districts of East resulting output difference of an hour, but they are in fact a Date object represents the same time

public class TestDate {

    public static void main(String[] args) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date(1572960924868L);//以时间戳来给定一个特定时刻
        System.out.println(sdf.format(date));
    }
}

Five, java time zone conversion

SimpleDateFormat objects can set the time zone, a zone conversion Date object so that the same can be performed by SimpleDateFormat

    @Test
    public void test2(){
        Date date=new Date(1572960924868L);
        SimpleDateFormat sdf8=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf8.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));//设置时区为东八区
        System.out.println("东八区的时间:"+sdf8.format(date));//输出格式化日期

        SimpleDateFormat sdf9=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf9.setTimeZone(TimeZone.getTimeZone("GMT+9:00"));//设置时区为东八区
        System.out.println("东九区的时间:"+sdf9.format(date));//输出格式化日期
    }

FIG result output follows

1572960924868 same time, converted to the Date object, and then by conversion of the SimpleDateFormat time zones, and time string formatted output.

So in practical applications, problems to be encountered in storage time, can be stored in the database corresponding time stamp so when the database storage area would not be a problem sometimes, but when the time required in the application to use, this timestamp put into Date, then by time SimpleDateFormat converted into the desired time zone.

Guess you like

Origin www.cnblogs.com/chengxuxiaoyuan/p/11802241.html