📜  Java.util.TimeZone 类 |设置 1

📅  最后修改于: 2022-05-13 01:54:18.936000             🧑  作者: Mango

Java.util.TimeZone 类 |设置 1

TimeZone 类用于表示时区偏移,也用于计算夏令时。
什么是时区和时间偏移?
“时区”用于描述世界不同地区的当前时间。它是指世界上按经度划分的24个区域中的特定区域之一。在这些区域中的每一个区域内,都会维护一个标准版本的时间。

  • 不同时区是根据它们与协调世界时或 UTC 的关系来计算的。
  • 时间偏移量是从通用时间中减去或添加到当前民用时间的时间量,无论是标准时间还是夏令时 (DST)。
  • 我们根据经度将整个地球从东到西分为 24 个不同的区域,因此每个区域宽 15 度。因此,地球上有 24 个不同的时区可用。每个时区宽 15 度,每个时区相差一小时。
  • 根据从格林威治子午线向东或向西的距离,您必须为经度中的每 15 度间隔添加或减去适当的时间。

例如:要以小时为单位查找特定位置的时区,您可以将经度以度数除以 15。因此,例如,105° E 将是 105/15,等于 7。这转化为时间时区比 UTC 或 GMT 时间早 7 小时,也可以标记为 UTC+7。其中 7 是该位置的时间偏移量。

Java中的时区类

类声明

public abstract class TimeZone extends 
Object implements Serializable, Cloneable

TimeZone 类的方法:

  • getAvailableIDs() – 使用此方法可以获得所有可用的时区 ID。
Syntax : public static String[] getAvailableIDs()
  • getAvailableIDs (int rawOffset) – 使用此方法,您可以获得一个 ID 数组,其中该 ID 的时区具有以毫秒为单位的指定 GMT 偏移量。
Syntax : public static String[] getAvailableIDs(int rawOffset)
Parameters: rawOffset - the given time zone GMT offset in milliseconds.
Java
// Java program for Demonstration of
// getAvailableIDs() and
// getAvailableIDs(int rawOffset ) methods
import java.util.TimeZone;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // get all the  timezones ids defined by TimeZone class
        String[] availableTimezones = TimeZone.getAvailableIDs();
 
        // Print Total no of TimeZones
        System.out.println("Total No of Time Zone Available");
 
        System.out.println(availableTimezones.length);
 
        // get all the  timezones  whose offset is
        // 7200000 milliseconds means 2 hour
        String[] timezones = TimeZone.getAvailableIDs(7200000);
 
        // Print Total no of TimeZones
        System.out.println("No of Time Zone having time offset 2 hour");
 
        System.out.println(timezones.length);
 
        // print all timezones names
        System.out.println("Timezone names having time offset 2 hour");
 
        for (int i = 0; i < timezones.length; i++)
            System.out.println(timezones[i]);
    }
}


Java
// Java program for Demonstration of
// getDefault() and getDisplayName() methods
import java.util.TimeZone;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // Get your Local Time Zone Where this Program is Running.
        TimeZone timezone = TimeZone.getDefault();
 
        // Get the Name of Time Zone
        String LocalTimeZoneDisplayName = timezone.getDisplayName();
 
        // Print the Name of Time Zone
        System.out.println(LocalTimeZoneDisplayName);
    }
}


Java
// Java program for Demonstration of
// getTimeZone(String ID),
// getDSTSavings()  and getID() methods
import java.sql.Time;
import java.util.TimeZone;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // creating Timezone object whose id is Europe/Berlin
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        // printing the Display Name of this timezone object
        System.out.println("Display Name");
 
        System.out.println(timezone.getDisplayName());
 
        // getting DST in milliseconds
        int timeInMilliseconds = timezone.getDSTSavings();
 
        System.out.println("\nDST of Europe/Berlin is");
        System.out.println(timezone.getDSTSavings());
 
        // get Id of your Default Time Zone
        TimeZone defaultTimezone = TimeZone.getDefault();
 
        System.out.println("\nThe id of default Time zone is");
        System.out.println(timezone.getID());
    }
}


Java
// Java program for
// Demonstration of getOffset(long date),
// inDaylightTime(Date date)  and
// observesDaylightTime() methods
import java.sql.Time;
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // creating Timezone object whose id is Europe/Berlin
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        // printing offset value
        System.out.println("Offset value of Europe/Berlin:");
 
        System.out.println(timezone.getOffset(Calendar.ZONE_OFFSET));
 
        // create Date Object
        Date date = new Date(2017, 04, 16);
 
        // checking the date is in day light time of that Time Zone or not
        System.out.println("\nDate 16/04/2017 is in Day Light Time of");
 
        System.out.println("Timezone: timezone.getDisplayName()");
        System.out.println(timezone.inDaylightTime(date));
 
        // check this Time Zone observes Day Light Time or Not
        System.out.println("\nTimeZone name " + timezone.getDisplayName());
 
        System.out.println("Observes Day Light Time");
        System.out.println(timezone.observesDaylightTime());
    }
}


Java
// Java program for Demonstration of
// setDefault(TimeZone zone),
// setID(String ID)  and clone() methods
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // My previous Default Time Zone is
        TimeZone DefaultTimeZone = TimeZone.getDefault();
 
        System.out.println("Current Default TimeZone:");
        System.out.println(DefaultTimeZone.getDisplayName());
 
        // Setting  Europe/Berlin as your Default Time Zone
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        timezone.setDefault(timezone);
        TimeZone NewDefaultTimeZone = TimeZone.getDefault();
        System.out.println("\nNew Default TimeZone:");
        System.out.println(NewDefaultTimeZone.getDisplayName());
 
        // change Id Europe/Berlin to Eur/Ber
        timezone.setID("Eur/Ber");
 
        System.out.println("\nNew Id of Europe/Berlin is");
        System.out.println(timezone.getID());
 
        // create copy of a time zone
        System.out.println("\nOriginal TimeZone ID:");
 
        System.out.println(timezone.getID());
        TimeZone clonedTimezone = (TimeZone)timezone.clone();
        System.out.println("Cloned TimeZone ID:");
        System.out.println(clonedTimezone.getID());
    }
}


Java
// Java program to illustrate
// java.util.timezone class
import java.text.*;
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
        // Get your Local Time Zone Where this Program is Running.
        TimeZone timezone = TimeZone.getDefault();
 
        // Get the Name of Time Zone
        String LocalTimeZoneName = timezone.getDisplayName();
 
        // Initialize your Date Object and Date Format to represent your Date
        Date date = new Date();
        DateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        // set your local time Zone to your Date Format time Zone
        dformat.setTimeZone(timezone);
 
        // Print Date and Time for your Time Zone
        System.out.println("Date and time of your Local Time Zone:");
        System.out.println(LocalTimeZoneName + ", " + dformat.format(date));
    }
}


Output:  
Total No of Time Zone Available
628
No of Time Zone having a time offset 2 hour
43
Timezone names having a time offset 2 hour
ART
Africa/Blantyre
Africa/Bujumbura
Africa/Cairo......
............
  • getDefault() - 使用此方法,您可以获得程序正在运行的地方的时区。
Syntax : public static TimeZone getDefault()
  • getDisplayName() – 方法返回初始化 TimeZone 的长标准时间名称。
Syntax : public final String getDisplayName() 

Java

// Java program for Demonstration of
// getDefault() and getDisplayName() methods
import java.util.TimeZone;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // Get your Local Time Zone Where this Program is Running.
        TimeZone timezone = TimeZone.getDefault();
 
        // Get the Name of Time Zone
        String LocalTimeZoneDisplayName = timezone.getDisplayName();
 
        // Print the Name of Time Zone
        System.out.println(LocalTimeZoneDisplayName);
    }
}
Output:  
Coordinated Universal Time
  • getTimeZone(String ID) – 此方法用于获取给定 ID 的时区。
Syntax :public static TimeZone getTimeZone(String ID)
Parameters: ID - the ID for a TimeZone.
  • getDSTSavings() – 方法返回要添加到本地标准时间以获取本地挂钟时间的时间量。
Syntax : public int getDSTSavings()
  • getID() - 此方法用于获取该时区的 ID。
Syntax : public String getID()

Java

// Java program for Demonstration of
// getTimeZone(String ID),
// getDSTSavings()  and getID() methods
import java.sql.Time;
import java.util.TimeZone;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // creating Timezone object whose id is Europe/Berlin
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        // printing the Display Name of this timezone object
        System.out.println("Display Name");
 
        System.out.println(timezone.getDisplayName());
 
        // getting DST in milliseconds
        int timeInMilliseconds = timezone.getDSTSavings();
 
        System.out.println("\nDST of Europe/Berlin is");
        System.out.println(timezone.getDSTSavings());
 
        // get Id of your Default Time Zone
        TimeZone defaultTimezone = TimeZone.getDefault();
 
        System.out.println("\nThe id of default Time zone is");
        System.out.println(timezone.getID());
    }
}
Output: 
Display Name
Central European Time

DST of Europe/Berlin is
3600000

The id of default Time zone is
Europe/Berlin
  • getOffset(long date) – 该方法用于返回该时区在方法中传递的日期与 UTC 的偏移量。
Syntax : the method is used to return the offset of this time zone
 from UTC at the passed date in method.
Parameters: date - the date represented in milliseconds
 since January 1, 1970 00:00:00 GMT
  • inDaylightTime(Date date) - 如果给定日期在该时区的夏令时,则此方法返回 true,否则返回 false。
Syntax :Syntax : public abstract boolean inDaylightTime(Date date)
Parameters:date - the given Date.
  • observesDaylightTime() – 如果此 TimeZone 当前处于夏令时,或者如果在未来任何时间发生从标准时间到夏令时的转换,则此方法返回 true。
Syntax :public boolean observesDaylightTime()

Java

// Java program for
// Demonstration of getOffset(long date),
// inDaylightTime(Date date)  and
// observesDaylightTime() methods
import java.sql.Time;
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // creating Timezone object whose id is Europe/Berlin
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        // printing offset value
        System.out.println("Offset value of Europe/Berlin:");
 
        System.out.println(timezone.getOffset(Calendar.ZONE_OFFSET));
 
        // create Date Object
        Date date = new Date(2017, 04, 16);
 
        // checking the date is in day light time of that Time Zone or not
        System.out.println("\nDate 16/04/2017 is in Day Light Time of");
 
        System.out.println("Timezone: timezone.getDisplayName()");
        System.out.println(timezone.inDaylightTime(date));
 
        // check this Time Zone observes Day Light Time or Not
        System.out.println("\nTimeZone name " + timezone.getDisplayName());
 
        System.out.println("Observes Day Light Time");
        System.out.println(timezone.observesDaylightTime());
    }
}
Output:
Offset value of Europe/Berlin:
3600000

Date 16/04/2017 is in Day Light Time of
Timezone: timezone.getDisplayName()
true

TimeZone name Central European Time
Observes Day Light Time
true
  • setDefault(TimeZone zone) - 用于设置 getDefault 方法返回的 TimeZone。
Syntax : public static void setDefault(TimeZone zone)
Parameters: zone - the new default time zone
  • setID(String ID) – 用于设置时区 ID。
Syntax :public void setID(String ID)
Parameters: ID - the new time zone ID.
  • clone() - 此方法用于创建此 TimeZone 的副本
Syntax : public Object clone()

Java

// Java program for Demonstration of
// setDefault(TimeZone zone),
// setID(String ID)  and clone() methods
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
 
        // My previous Default Time Zone is
        TimeZone DefaultTimeZone = TimeZone.getDefault();
 
        System.out.println("Current Default TimeZone:");
        System.out.println(DefaultTimeZone.getDisplayName());
 
        // Setting  Europe/Berlin as your Default Time Zone
        TimeZone timezone = TimeZone.getTimeZone("Europe/Berlin");
 
        timezone.setDefault(timezone);
        TimeZone NewDefaultTimeZone = TimeZone.getDefault();
        System.out.println("\nNew Default TimeZone:");
        System.out.println(NewDefaultTimeZone.getDisplayName());
 
        // change Id Europe/Berlin to Eur/Ber
        timezone.setID("Eur/Ber");
 
        System.out.println("\nNew Id of Europe/Berlin is");
        System.out.println(timezone.getID());
 
        // create copy of a time zone
        System.out.println("\nOriginal TimeZone ID:");
 
        System.out.println(timezone.getID());
        TimeZone clonedTimezone = (TimeZone)timezone.clone();
        System.out.println("Cloned TimeZone ID:");
        System.out.println(clonedTimezone.getID());
    }
}
Output:
Current Default TimeZone:
India Standard Time

New Default TimeZone:
Central European Time

New Id of Europe/Berlin is
Eur/Ber

Original TimeZone ID:
Eur/Ber
Cloned TimeZone ID:
Eur/Ber

示例:打印程序正在运行的任何给定输入时区的日期和时间。

Java

// Java program to illustrate
// java.util.timezone class
import java.text.*;
import java.util.*;
 
public class TimeZoneDemo {
 
    public static void main(String[] args)
    {
        // Get your Local Time Zone Where this Program is Running.
        TimeZone timezone = TimeZone.getDefault();
 
        // Get the Name of Time Zone
        String LocalTimeZoneName = timezone.getDisplayName();
 
        // Initialize your Date Object and Date Format to represent your Date
        Date date = new Date();
        DateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        // set your local time Zone to your Date Format time Zone
        dformat.setTimeZone(timezone);
 
        // Print Date and Time for your Time Zone
        System.out.println("Date and time of your Local Time Zone:");
        System.out.println(LocalTimeZoneName + ", " + dformat.format(date));
    }
}
Output:  
Date and time of your Local Time Zone:
Coordinated Universal Time, 2018-04-17 07:36:19

参考 – Oracle 文档