Java 时区 与 相关类

协调世界时_百度百科

UTC和GMT什么关系?

GMT 和 UTC 在一般使用的情況下沒有差別

GMT:Greenwich Mean Time 格林尼治标准时间。这是以英国格林尼治天文台观测结果得出的时间,这是英国格林尼治当地时间,这个地方的当地时间过去被当成世界标准的时间。 GMT 12:00 就是指的是英国伦敦郊区的皇家格林尼治天文台当地的中午12:00,而GMT+8 12:00,则是指的东八区的北京当地时间的12:00。 相邻GMT时区, 相隔1小时, -12到+12, 晚12h 到 早12h 由于地球自转不均匀不规则,导致GMT不精确,现在已经不再作为世界标准时间使用

UT:Universal Time 世界时。根据原子钟计算出来的时间。

UTC:Coordinated Universal Time 协调世界时。因为地球自转越来越慢,每年都会比前一年多出零点几秒,每隔几年协调世界时组织都会给世界时+1秒,让基于原子钟的世界时和基于天文学(人类感知)的格林尼治标准时间相差不至于太大。 并将得到的时间称为UTC,这是现在使用的世界标准时间。

协调世界时不与任何地区位置相关,也不代表此刻某地的时间,所以在说明某地时间时要加上时区也就是说GMT并不等于UTC,而是等于UTC+0, 只是格林尼治刚好在0时区上。GMT = UTC+0

utc并不是一个时间,而是一个标准,这个标准把世界分成utc-12到utc+12共24个时区。utc0才是一个时间

==================================

偏移量表示法 描述 GMT+8 相对GMT多8个小时 Etc/GMT-8 同GMT+8,+-号相反 UTC+8 同GMT+8 GMT+08:00 精确到分钟级别 GMT+08:00:00 精确到秒级别 GMT+0800 精确到分钟级别,省略冒号 GMT+080000 精确到秒级别,省略冒号 +08:00 精确到分钟级别,省略前缀 +08:00:00 精确到秒级别,誉略前缀 +0800 精确到分钟级别,省略前缀与冒号 +080000 精确到秒级别,省略前缀与冒号 Z 表示零时区,等同于GMT、UTC、GMT+0、UTC+0

时区的区域表示法

除了用偏移量来表示时区,为了方便,人们还按区域/城市的方式来定义时区,如Asia/Shanghai,Asia/Hong_Kong都表示东8区,具体有哪些城市命名的时区,可以在时区数据库中查看。

另外,为了简化区域时区表示法,又定义了一套时区缩写,如CST是中国时区China Standard Time的缩写,可以在时区缩写中查看各种缩写定义。

注意,一般都不建议使用时区缩写,因为时区缩写的命名经常会重复,比如CST是Central Standard Time(北美中部标准时间UTC -6)、China Standard Time(中国标准时间UTC +8)、Cuba Standard Time(古巴标准时间UTC -5)。

由于不同软件对CST的解释可能不同,导致会出现时间相差13或14个小时的情况,这在Java搭配MySQL时经常出现,我还专门写了一篇文章mysql的timestamp会存在时区问题?,对于一定要使用时区缩写的场景,可以使用香港时区缩写HKT,它不重复且和上海处于同一个时区。

百度安全验证

=============================

https://blog.csdn.net/qq_36066039/article/details/127296818

不要把字符串看成时间,应该看成某个时区的显示时间。

=============================

CST

CST因代表4个不同的地方,故时区有4个,分别是UT-6:00、UT+9:30、UT+8:00和 UT-4:00

一般不用

=============================

什么是夏令时

DST(Daylight Saving Time),夏令时又称夏季时间,或者夏时制。

它是为节约能源而人为规定地方时间的制度。一般在天亮早的夏季人为将时间提前一小时,可以使人早起早睡,减少照明量,以充分利用光照资源,从而节约照明用电。

全球约40%的国家在夏季使用夏令时,其他国家则全年只使用标准时间。标准时间在有的国家也因此被相应地称为冬季时间。

在施行夏令时的国家,一年里面有一天只有23小时(夏令时开始那一天),有一天有25小时(夏令时结束那一天),其他时间每天都是24小时。

夏令时的争议

从过去的100多年来看,夏令时往往是在国家发生严重危机(如战争和能源短缺)的情况下才会受到青睐。而在相对和平的近10年里,这种时间制度则变得越来越不受欢迎。

它会使得人们的生物钟被扰乱,常常陷入睡眠不足的情况,不仅对人体健康有害、导致车祸,还会对旅游、航空领域造成极大的混乱。

(2019年)3月26日,作为全世界第一个提出并实行夏令时的国家,德国,在欧洲议会上以410比192的赞成票通过了取消冬、夏令时转换制提案,拟定于2021年4月起,所有欧盟国家不再实行冬、夏令时转换。待各成员国形成最终法案后,将选择永久使用夏令时时间或是冬令时时间。

=============================

本地时间

在日常生活中所使用的时间我们通常称之为本地时间。这个时间等于我们所在(或者所使用)时区内的当地时间,它由与世界标准时间(UTC)之间的偏移量来定义。这个偏移量可以表示为 UTC- 或 UTC+,后面接上偏移的小时和分钟数。

=============================

时间戳分为10位, 13位, 13位是, 毫秒级

由于我们地理位置处于东8时区, 所以 时间戳的 开始时间是, 1970-01-01 08:00:00

==========================

java 8 中的 ZoneOffset 与 ZoneId

ZoneOffset 表示 GMT/UTC 时区的偏移量,而 ZoneId 表示 UTC 的时区 ZoneOffset 继承自 ZoneId

所以转换时要先获得 id-> 再获得 offset

ZoneId.systemDefault(); // 底层是 TimeZone.getDefault().toZoneId()
ZoneId.of(xxx); // 能够基于多种规则解析, Z 代表UTC
​
ZoneOffset.of(xxx); // 解析偏移的时间
ZoneOffset.ofHours(8);

=======================

https://juejin.cn/post/6897143007108136973

时区的偏移量, 会随着政府的法令进行更改

======================

TimeZone

TimeZone 表示时区偏移量,也可以计算夏令时。

TimeZone tz = TimeZone.getDefault()

TimeZone.getTimeZone("GMT+8:00"); // 获取 “GMT+08:00”对应的时区

TimeZone chongqing = TimeZone.getTimeZone("Asia/Chongqing"); // 获取 “中国/重庆”对应的时区

======================

ZoneInfo

ZoneInfo是TimeZone的实现子类,表示时区的 GMT 偏移和夏令时转换。

======================

LocalDateTime ZonedDateTime

LocalDateTime

表示当前日期时间,相当于:yyyy-MM-ddTHH:mm:ss

获取当前日期和时间
LocalDate d = LocalDate.now(); // 当前日期, 使用的是getSystemTimeZoneID({jdk路径}); 系统默认时区
LocalTime t = LocalTime.now(); // 当前时间
LocalDateTime dt = LocalDateTime.now(); // 当前日期和时间
​
// 默认Asia/shanghai
ZoneId zone = ZoneId.systemDefault();
ZoneId zone = TimeZone.getDefault().toZoneId();
// GMT==> offset偏移8*3600
​
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC)); // 手动设置时区为UTC0, 2者效果是一样的
// utc0==> offset 偏移0
与秒/毫秒 互转换
TimeUtils.toSeconds(now);
TimeUtils.toMilliSecond(now);
​
localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochSecond(); // 纪元秒
localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli(); // 纪元毫秒
​
或者 通过Instant 再转成Date 再getTime();
String互转
//时间转字符串格式化
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
 String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
​
//字符串转时间
String dateTimeStr = "2018-07-28 14:11:15";
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);

获取指定日期和时间
LocalDate d2 = LocalDate.of(2021, 07, 14); // 2021-07-14, 注意07=07月
LocalTime t2 = LocalTime.of(13, 14, 20); // 13:14:20
LocalDateTime dt1 = LocalDateTime.of(2021, 07, 14, 13, 14, 20);
LocalDateTime dt2 = LocalDateTime.of(d2, t2);
日期时间的加减法及修改
LocalDateTime currentTime = LocalDateTime.now(); // 当前日期和时间
System.out.println("------------------时间的加减法及修改-----------------------");
//3.LocalDateTime的加减法包含了LocalDate和LocalTime的所有加减,上面说过,这里就只做简单介绍
System.out.println("3.当前时间:" + currentTime);
System.out.println("3.当前时间加5年:" + currentTime.plusYears(5));
System.out.println("3.当前时间加2个月:" + currentTime.plusMonths(2));
System.out.println("3.当前时间减2天:" + currentTime.minusDays(2));
System.out.println("3.当前时间减5个小时:" + currentTime.minusHours(5));
System.out.println("3.当前时间加5分钟:" + currentTime.plusMinutes(5));
System.out.println("3.当前时间加20秒:" + currentTime.plusSeconds(20));
//还可以灵活运用比如:向后加一年,向前减一天,向后加2个小时,向前减5分钟,可以进行连写
System.out.println("3.同时修改(向后加一年,向前减一天,向后加2个小时,向前减5分钟):" + currentTime.plusYears(1).minusDays(1).plusHours(2).minusMinutes(5));
System.out.println("3.修改年为2025年:" + currentTime.withYear(2025));
System.out.println("3.修改月为12月:" + currentTime.withMonth(12));
System.out.println("3.修改日为27日:" + currentTime.withDayOfMonth(27));
System.out.println("3.修改小时为12:" + currentTime.withHour(12));
System.out.println("3.修改分钟为12:" + currentTime.withMinute(12));
System.out.println("3.修改秒为12:" + currentTime.withSecond(12));

LocalDateTime和Date相互转化

Date转LocalDateTime
System.out.println("------------------方法一:分步写-----------------------");
//实例化一个时间对象
Date date = new Date();
//返回表示时间轴上同一点的瞬间作为日期对象
Instant instant = date.toInstant();
//获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
//根据时区获取带时区的日期和时间
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
//转化为LocalDateTime
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.println("方法一:原Date = " + date);
System.out.println("方法一:转化后的LocalDateTime = " + localDateTime);
​
System.out.println("------------------方法二:一步到位(推荐使用)-----------------------");
//实例化一个时间对象
Date todayDate = new Date();
//Instant.ofEpochMilli(long l)使用1970-01-01T00:00:00Z的纪元中的毫秒来获取Instant的实例
LocalDateTime ldt = Instant.ofEpochMilli(todayDate.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("方法二:原Date = " + todayDate);
System.out.println("方法二:转化后的LocalDateTime = " + ldt);
LocalDateTime转Date
System.out.println("------------------方法一:分步写-----------------------");
//获取LocalDateTime对象,当前时间
LocalDateTime localDateTime = LocalDateTime.now();
//获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
//根据时区获取带时区的日期和时间
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
//返回表示时间轴上同一点的瞬间作为日期对象
Instant instant = zonedDateTime.toInstant();
//转化为Date
Date date = Date.from(instant);
System.out.println("方法一:原LocalDateTime = " + localDateTime);
System.out.println("方法一:转化后的Date = " + date);
​
System.out.println("------------------方法二:一步到位(推荐使用)-----------------------");
//实例化一个LocalDateTime对象
LocalDateTime now = LocalDateTime.now();
//转化为date
Date dateResult = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("方法二:原LocalDateTime = " + now);
System.out.println("方法二:转化后的Date = " + dateResult);
​

线程安全的原因?

因为成员字段 都是final修饰的, 且 引用类型的成员也是不可变的

======================

ZonedDateTime

表示一个带时区的日期和时间,ZonedDateTime可以理解为LocalDateTime+ZoneId

从源码可以看出来,ZonedDateTime类中定义了LocalDateTime和ZoneId两个变量。

方便根据时区来 转换时间字符串

获取当前时间+带时区+时区转换
// 默认时区获取当前时间
ZonedDateTime zonedDateTime = ZonedDateTime.now();
// 用指定时区获取当前时间,Asia/Shanghai为上海时区
ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
//withZoneSameInstant为转换时区,参数为ZoneId
ZonedDateTime zonedDateTime2 = zonedDateTime.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(zonedDateTime);
System.out.println(zonedDateTime1);
System.out.println(zonedDateTime2);
​
LocalDateTime+ZoneId变ZonedDateTime
LocalDateTime localDateTime = LocalDateTime.now();
ZonedDateTime zonedDateTime1 = localDateTime.atZone(ZoneId.systemDefault());
ZonedDateTime zonedDateTime2 = localDateTime.atZone(ZoneId.of("America/New_York"));
System.out.println(zonedDateTime1);
System.out.println(zonedDateTime2);
​

======================

Instant

精确到纳秒级别 Instant 代表的是一个绝对时间,并不包括时区的概念,所以必须传入的是符合 UTC格式的字符串

Instant是没有时区的,但是Instant加上时区后,可以转化为ZonedDateTime

问: 当前时间上加上5个小时4分钟。那么这个例子中,使用了多少个 java.time.Instant 实例呢?

答: 是两个。Java.time 这个包是线程安全的,并且和其他大部分类一样,是不可变类。Instant 也遵守这个规则,因此 plus() 方法会产生一个新的实例,如:

Instant instant1 = instant.plus(Duration.ofHours(5).plusMinutes(4)); 
System.out.println(instant == instant1); // false

Java时间戳类Instant的使用详解_java_脚本之家

Instant实例化

常用api

// 获取当前时间
Instant instant1 = Instant.now();
 
// 字符串转Instant, 必须是utc时间格式
// UTC时间格式为:YYYY-MM-DDThh:mm:ssZ。例如,2014-11-11T12:00:00Z(为北京时间2014年11月11日20点0分0秒)
// 底层调用的是 DateTimeFormatter.ISO_INSTANT 来格式化的
Instant instant2 = Instant.parse("2022-09-26T03:46:24.373Z");
 
// 构建秒级Instant对象,从时间1970-01-01T00:00:00Z开始计算(距离原点5000秒)
// 结果为:1970-01-01T01:23:20Z
Instant instant3 = Instant.ofEpochSecond(5000);
 
// 构建毫秒级Instant对象,同样从时间1970-01-01T00:00:00Z开始计算(距离原点5000毫秒)
// 结果为:1970-01-01T00:00:05Z
Instant instant4 = Instant.ofEpochMilli(5000);
//将java.util.Date转换为Instant 
Instant instant = Instant.ofEpochMilli(new Date().getTime()); 
​
// 构建纳秒级Instant对象,同样从时间1970-01-01T00:00:00Z开始计算
// 参数:epochSecond(秒),nanoAdjustment(纳秒)
// 结果为:1970-01-01T00:00:05.000001111Z
Instant instant5 = Instant.ofEpochSecond(5, 1111);

Instant获取 不同单位的时间戳

Instant instant = Instant.now();
// 时区相差8小时 2022-09-26T07:04:19.110Z
System.out.println(instant);
 
System.out.println("秒:"+instant.getEpochSecond());
// 1秒 = 1000 毫秒
System.out.println("毫秒:"+instant.toEpochMilli());
// 1毫秒 = 1000 000 纳秒
System.out.println("纳秒:"+instant.getNano());

Instant时间点比较

Instant instant1 = Instant.parse("2022-11-26T07:04:19.110Z");
Instant instant2 = Instant.parse("2022-11-26T07:04:19.110Z");
Instant instant3 = Instant.parse("2022-08-26T07:04:19.110Z");
 
// 相等为0
System.out.println(instant1.compareTo(instant2));
// instant1大于instant3 为1
System.out.println(instant1.compareTo(instant3));
// instant1小于instant3 为-1
System.out.println(instant3.compareTo(instant1));
 
// true
System.out.println(instant1.isAfter(instant3));
// false
System.out.println(instant1.isBefore(instant3));
​

Instant时间点运算

Instant instant1 = Instant.parse("2022-09-26T07:04:19.110Z");
 
// 在instant1的基础上增加2秒,值为:2022-09-26T07:04:21.110Z
System.out.println(instant1.plusSeconds(2));
 
// 在instant1的基础上增加1毫秒,值为:2022-09-26T07:04:19.111Z
System.out.println(instant1.plusMillis(1));
 
// 在instant1的基础上增加1001纳秒,值为:2022-09-26T07:04:19.110001001Z
System.out.println(instant1.plusNanos(1001));
 
// 在instant1的基础上增加1秒,值为:2022-09-26T07:04:20.110Z
// 该值取决于后面指定的单位,可以从ChronoUnit枚举类获取
System.out.println(instant1.plus(1, ChronoUnit.SECONDS));
 
// 在instant1的基础上减去1秒,值为:2022-09-26T07:04:18.110Z
// plus是增加,minus是减少,逻辑类似可以参考上面plus相关A
System.out.println(instant1.minusSeconds(1));

Instant时间点计算时需要注意,无论是调用plus或者minus相关API都会重新创建新对象。

存在的坑

Instant.now()获取的时间与北京时间相差8个时区,这是一个细节,要避坑。

看源码,用的是UTC时间

public static Instant now() { return Clock.systemUTC().instant(); }

解决办法: 手动加回来

Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));

===========

TimeUnit

包含了各种时间粒度

各种粒度之间的转换

//把3天转化成小时, 结果是:72   
System.out.println( TimeUnit.HOURS.convert( 3 , TimeUnit.DAYS ) );  

sleep写法

常用的时间段, 常量

===========

Period

  • 用于LocalDate之间的比较

  • 主要是Period类方法getYears(),getMonths()和getDays()来计算,只有年、月、日

===========

Duration

  • 用于LocalDateTime之间的比较

  • 只有日、时、分、秒、毫秒

Duration主要用来衡量秒级和纳秒级的时间,使用于时间精度要求比较高的情况。

Duration d1= Duration.of(2, ChronoUnit.DAYS);
​
Duration d2= Duration.ofHours(10);
​
Duration.between(d1, d2).getSeconds();

===========

ChronoUnit

  • ChronoUnit类计算年、月、周、日、时、分、秒、毫秒

ChronoUnit是用来表示时间单位的,但是也提供了一些非常有用的between方法来计算两个时间的差值

System.out.println("相差的天数: " + ChronoUnit.DAYS.between(day1, today));

底层用的是 Duration

=============

TemporalUnit

是接口, 实现类是 ChronoUnit

=============

Until方法

同ChronoUnit类一样,计算年、月、周、日、时、分、秒、毫秒

startTime.until(endTime, ChronoUnit.YEARS);

=============

SimpleDateFormat

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd  HH:mm:ss");
​
 System.out.println(simpleDateFormat.format(new Date()));
​
 System.out.println(simpleDateFormat.parse("2018-07-09 11:10:21"));
​
作者:咖啡拿铁
链接:https://juejin.cn/post/6844903640398987272

SimpleDateFormat 是线程不安全的原因? 答: 在SimpleDateFormat源码中,所有的格式化和解析都需要通过一个中间对象进行转换,那就是Calendar

  • 避开的方式有: new新建对象

  • ThreadLocal 每个线程持有一个对象

  • 三方工具包(Joda-Time, common-lang包)

    1、不要定义为static变量,使用局部变量

    2、加锁:synchronized锁和Lock锁

    3、使用ThreadLocal方式

    4、使用DateTimeFormatter代替SimpleDateFormat(DateTimeFormatter是线程安全的,java 8+支持)

    5、使用FastDateFormat 替换SimpleDateFormat(FastDateFormat 是线程安全的,Apache Commons Lang包支持,不受限于java版本)

使用java8的时间类型

  • LocalDate无法包含时间;

  • LocalTime无法包含日期;

  • LocalDateTime才能同时包含日期和时间。

===========

DateTimeFormatter

常用的用法

ZonedDateTime zonedDateTime = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm ZZZZ");
System.out.println(formatter.format(zonedDateTime));
​
DateTimeFormatter usFormatter = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US);
System.out.println(usFormatter.format(zonedDateTime));
​
DateTimeFormatter chinaFormatter = DateTimeFormatter.ofPattern("yyyy MMM dd EE HH:mm", Locale.CHINA);
System.out.println(chinaFormatter.format(zonedDateTime));
​
// DateTimeFormatter
​
// '2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'
ISO_DATE_TIME
​
// '2011-12-03T10:15:30Z'  Z表示是标准时间
ISO_INSTANT
​
// '10:15' or '10:15:30'   没有偏移值
ISO_LOCAL_TIME
​
// '2011-12-03T10:15:30+01:00'
ISO_OFFSET_DATE_TIME
    
// '10:15', '10:15:30' or '10:15:30+01:00'
ISO_TIME
​
// 2011-12-03T10:15:30+01:00[Europe/Paris]
ISO_ZONED_DATE_TIME 
​
其他略

存在的坑

在正常配置按照标准格式的字符串日期,是能够正常转换的。如果月,日,时,分,秒在不足两位的情况需要补0,否则的话会转换失败,抛出异常。

举例
“yyyy-MM-dd HH:mm:ss.SSS”
​
“2021-7-20 23:46:43.946”
​
中间的月份格式是MM,实际时间是7
​
解决方案:保持格式字符串与实际的时间匹配

YYYY和DD谨慎使用 YYYY是取的当前周所在的年份,week-based year 是 ISO 8601 规定的。2020年12月31号,周算年份,就是2021年 DD代表的是处于这一年中那一天,不是处于这个月的那一天

===========

旧版本api存在的问题

1、tostring()输出时,总以系统的默认时区格式输出,不友好。

2、时区不能转换

3、日期和时间的计算不简便,例如计算加减,比较两个日期差几天等。

4、格式化日期和时间的SimpleDateFormat对象是线程不安全的

5、Date对象本身也是线程不安全的

Calendar类说明

Calendar类提供了获取或设置各种日历字段的各种方法,比Date类多了一个可以计算日期和时间的功能。

存在的问题

月份计算时,要+1;

星期是从周日开始计算,周日为1,1~7表示星期;

getWeekYear()读取年份,在跨年那周的时候,程序获取的年份可能不是我们想要的,例如在2019年12月30号时,要返回2019,结果是返回2020 正确做法: 使用Calendar类 get(Calendar.YEAR)获取年份

=============

其他帮助文档

https://juejin.cn/post/6844903640398987272

springBoot 项目级别的 时间格式化设置

https://juejin.cn/post/6844903917898170382

万字博文教你搞懂java源码的日期和时间相关用法

万字博文教你搞懂java源码的日期和时间相关用法_could not be parsed at index 0-CSDN博客

  • 29
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的TimeZone类获取客户端时区信息。 在服务器端Java类中,可以通过获取HTTP请求头中的"TimeZone"信息来获取客户端时区。具体实现如下: ```java import java.util.TimeZone; public class TimeZoneUtil { public static String getClientTimeZone(HttpServletRequest request) { String timeZone = request.getHeader("TimeZone"); if (timeZone == null || timeZone.isEmpty()) { return "UTC"; } return timeZone; } public static TimeZone getTimeZone(String timeZone) { if (timeZone == null || timeZone.isEmpty()) { return TimeZone.getTimeZone("UTC"); } return TimeZone.getTimeZone(timeZone); } } ``` 在上面的代码中,getClientTimeZone方法会获取HTTP请求头中的"TimeZone"信息,如果没有该信息则默认返回UTC时区。 getTimeZone方法根据传入的时区信息获取对应的TimeZone对象,如果传入的时区信息为空,则默认返回UTC时区。 使用示例: ```java import java.util.TimeZone; public class Main { public static void main(String[] args) { // 假设客户端时区为东八区 String clientTimeZone = "Asia/Shanghai"; // 获取对应的TimeZone对象 TimeZone timeZone = TimeZoneUtil.getTimeZone(clientTimeZone); System.out.println(timeZone.getID()); // 输出: Asia/Shanghai System.out.println(timeZone.getDisplayName()); // 输出: 中国标准时间 System.out.println(timeZone.getRawOffset()); // 输出: 28800000 } } ``` 在上面的示例中,我们传入客户端时区为"Asia/Shanghai",使用TimeZoneUtil类获取对应的TimeZone对象,并输出相关信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值