首页 > 其他分享 >TimeZone

TimeZone

时间:2023-12-20 17:12:52浏览次数:33  
标签:String zone zoneID time TimeZone ID

概述

<code>TimeZone</code> represents a time zone offset, and also figures out daylight savings.

Typically, you get a <code>TimeZone</code> using <code>getDefault</code> which creates a <code>TimeZone</code> based on the time zone where the program is running.
For example, for a program running in Japan, <code>getDefault</code> creates a <code>TimeZone</code> object based on Japanese Standard Time.

TimeZone代表一个 时间区域偏移

通常使用getDefault可以获得一个TimeZone(根据程序运行时的时区);

比如,在日本运行的程序,会获取基于日本标准时间的TimeZone;

TimeZone timeZone = TimeZone.getDefault();
        String timeZoneID = timeZone.getID();
        System.out.println(timeZoneID); // Asia/Shanghai

        String displayName = timeZone.getDisplayName();
        System.out.println(displayName); // 中国标准时间

  

You can also get a <code>TimeZone</code> using <code>getTimeZone</code> along with a time zone ID.
For instance, the time zone ID for the U.S. Pacific Time zone is "America/Los_Angeles".
So, you can get a U.S. Pacific Time <code>TimeZone</code> object with:TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");

可以通过 getTimeZone获取指定的TimeZone

比如,America/Los_Angeles将会获取 美国/洛杉矶 的TimeZone;

You can use the <code>getAvailableIDs</code> method to iterate through all the supported time zone IDs.
You can then choose a supported ID to get a TimeZone.
If the time zone you want is not represented by one of the supported IDs, then a custom time zone ID can be specified to produce a TimeZone.

可以使用getAvailableIDs查看支持的time zone IDs

如果没有支持的,可以自定义时区ID

 

The syntax of a custom time zone ID is:

自定义时区ID语法:

...

 

 

abstract public class TimeZone implements Serializable, Cloneable {

        private static volatile TimeZone defaultTimeZone;
        private String           ID;

        static TimeZone getDefaultRef() {
            TimeZone defaultZone = defaultTimeZone;
            if (defaultZone == null) {
                // Need to initialize the default time zone.
                defaultZone = setDefaultZone();
                assert defaultZone != null;
            }
            // Don't clone here.
            return defaultZone;
        }

        private static synchronized TimeZone setDefaultZone() {
            TimeZone tz;
            // get the time zone ID from the system properties
            String zoneID = AccessController.doPrivileged(new GetPropertyAction("user.timezone"));

            // if the time zone ID is not set (yet), perform the
            // platform to Java time zone ID mapping.
            if (zoneID == null || zoneID.isEmpty()) {
                String javaHome = AccessController.doPrivileged(new GetPropertyAction("java.home"));
                try {
                    zoneID = getSystemTimeZoneID(javaHome);
                    if (zoneID == null) {
                        zoneID = GMT_ID;
                    }
                } catch (NullPointerException e) {
                    zoneID = GMT_ID;
                }
            }

            // Get the time zone for zoneID. But not fall back to
            // "GMT" here.
            tz = getTimeZone(zoneID, false);

            if (tz == null) {
                // If the given zone ID is unknown in Java, try to
                // get the GMT-offset-based time zone ID,
                // a.k.a. custom time zone ID (e.g., "GMT-08:00").
                String gmtOffsetID = getSystemGMTOffsetID();
                if (gmtOffsetID != null) {
                    zoneID = gmtOffsetID;
                }
                tz = getTimeZone(zoneID, true);
            }
            assert tz != null;

            final String id = zoneID;
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
                @Override
                public Void run() {
                    System.setProperty("user.timezone", id);
                    return null;
                }
            });

            defaultTimeZone = tz;
            return tz;
        }

        public static TimeZone getDefault() {
            return (TimeZone) getDefaultRef().clone();
        }

        public String getID()
        {
            return ID;
        }

        public static synchronized String[] getAvailableIDs() {
            return ZoneInfo.getAvailableIDs();
        }
    }

  

 

标签:String,zone,zoneID,time,TimeZone,ID
From: https://www.cnblogs.com/anpeiyong/p/17916998.html

相关文章

  • Timezone - 0001-01-01 00:00:00 UTC
    将一时间戳字段默认值设为0001-01-0100:00:00UTC,在timezone为东8区的数据库中字段值显示为:0001-01-0108:05:43+08:05:43 Google后找到如下解释:0001-01-0100:00:00 UTC.Andthatisindeed0001-01-0108:05:43inyourlocaltimezone(whateverthatmaybe,probably......
  • docker container中变更timezone
    当前使用了playwright官方python镜像: https://playwright.dev/python/docs/docker但在实际使用时,时间总是显示为UTC0时间 正好相差8个小时,前面是jenkins打印时间,后面部分是container内部时间查了网上各种方法,总共有几种:1,直接加命令行:dockerrun-eTZ=Asia/Shanghai2......
  • PostgreSQL - Change Timezone
    ThedefaulttimezoneofanewlycreateddatabaseisUTC. Youcansetthetimezonetoanewvaluesessionlyorglobally:zzh@ZZHPC:~$dockerexec-itpostgres16psql-Urootzimple_bankpsql(16.1)Type"help"forhelp.zimple_bank=#SELECTc......
  • C# 22H2之后的windows版本使用SetDynamicTimeZoneInformation设置时区失败处理
    使用SetDynamicTimeZoneInformation设置时区返回false,设置失败。使用PowerShell设置Set-TimeZone成功。///<summary>///设置本地时区///参数取值"ChinaStandardTime",即可设置为中国时区///</summary>///<paramname="timeZoneId"></param>///<retur......
  • ASP.NET Core – DateTime, DateTimeOffset, DateOnly, TimeOnly, TimeSpan, TimeZone
    前言心血来潮,这篇讲点基础的东西。对日期和时区TimeZone不熟悉的读者,请先看这篇 TimeZone,LeapYear,DateFormat,EpochTime时区,闰年,日期格式。 C#中的日期类型DateTime ......
  • 时间格式化以及指定时区(time&&timezone)
    工作中经常遇到按照指定格式的时间进行展示。可参考以下脚本逻辑满足需求Date.prototype.PtTimeByFormat=function(fmt){varo={"M+":this.getMonth()+1,//月份"d+":this.getDate(),//日"H+":this.getHours(),//小时"m+":this.getMi......
  • 时间格式化以及指定时区(time&&timezone)
    工作中经常遇到按照指定格式的时间进行展示。可参考以下脚本逻辑满足需求Date.prototype.PtTimeByFormat=function(fmt){varo={"M+":this.getMonth()+1,//月份"d+":this.getDate(),//日"H+":this.getHours(),//小时"m+":this.getMi......
  • 详解Jvm中时区设置方式,推荐 代码中TimeZone.getTimeZone("Asia/Shanghai") 而不使用Ti
    详解Jvm中时区设置方式原文链接:https://www.45fan.com/article.php?aid=20090934958860528675768691这篇文章memo一下Jvm中关于时区设定的基础操作。Java的时区设定这里列出如下三种方式方式说明TimeZone.setDefault方式通过java的utils下的TimeZone进行动态设定......
  • 【PHP兴趣部落-08】PHP中时区设置的三种方法(timezone)
    一、三种方法php中时区默认是格林尼治时间,和中国时差八个小时。现在根据需要将时间设置为中国时间,下面整理了三种方法。方法1:最好的方法在php.ini里加上找到date.timezone项,设置date.timezone=“Asia/Shanghai”,重启环境就ok了。方法2:在需要用到这些时间函数的时候,在页面添......
  • Python timezone package All In One
    PythontimezonepackageAllInOne中文日期格式化Asia/Shanghai#!/usr/bin/envpython3#coding:utf8importtimefromdatetimeimportdatetime,tzinfo,timezone#importpytz#❌ModuleNotFoundError:Nomodulenamed'pytz'#pip3installpytz#......