实战讲解时区处理基于SimpleDateFormat和LocalDateTime

1 缘起

最近在做海外的项目,
需要根据时区转换时间,起初,项目使用的时区格式为{area}/{city},
可直接使用SimpleDateFormat处理,后面由于要添加其他地区,
统一将时区改为UTC格式,此时,SimpleDateFormat功能首先,
于是使用LocalDateTime,分享如下,帮助有海外项目需求的开发者快速应用。

2 时区

为规范地球时间计量,统一计量标准,造福全球人类,
巨人们召开了会议,将全地球划分为24个时区,其中,
零时区是基准时区,以零时区分为东12区和西12区,
相邻时区时差为1小时,中国北京为例,位于东八区,即UTC+08:00,
向东,时间增加,向西,时间减少。
在这里插入图片描述

2.1 计时方式

全地球的计时方式:UTC、GMT
UTC:协调世界时,又称世界统一时间。
GMT:格林威治平时

2.2 时区格式

序号时区格式样例
1{area}/{city}Asia/Shanghai
2{UTC}{+/-}{HH:mm}UTC+08:00
3{GMT}{+/-}{HH:mm}GMT+08:00

从使用角度,UTC和GMT的差别是精度。

3 时区处理

3.1 处理{area}/{city}

使用SimpleDateFormat处理时区,样例如下,
需要注意的是,使用SimpleDateFormat无法指定当前时区,
只能使用默认的系统时区,当转换的时间跟随系统时区,可正常使用,
如果,转换的时间是包含的时区不是系统时区,则需要使用其他方法指定该时区。
同时,使用SimpleDateFormat转换UTC格式的时区时,
需要转换成GMT,才会得到正确的时间偏移。

package com.monkey.java_study.functiontest;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时区处理.
 *
 * @author xindaqi
 * @since 2022-11-01 16:04
 */
public class TimeTest {

    private static final Logger logger = LoggerFactory.getLogger(TimeTest.class);

    /**
     * 通过SimpleDateFormat处理时区,默认会根据当前系统的时区做转换,<br/>
     * 不支持设置当前时区,<br/>
     * 比如,当前系统时区为UTC+08:00,待转换的时区为UTC+05:30<br/>
     * 最终的时区为(以零时区为标准):UTC+02:30=(8:00-5:30)<br/>
     * (1){area}/{city}格式的时区:直接处理<br/>
     * (2)UTC{-/+}{HH:mm}格式的时区:转换成GMT{-/+}{HH:mm}格式<br/>
     *
     * @see SimpleDateFormat
     */
    @Test
    public void timeZoneTestKolkata() {
        String timeFormat = "yyyy-MM-dd HH:mm:ss";
        TimeZone areaCityTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
        logger.info(">>>>>>>>>Time zone:{}", areaCityTimeZone);
        String zone = "UTC+05:30".replace("UTC", "GMT");
        TimeZone gmtTimeZone = TimeZone.getTimeZone(ZoneId.of(zone));
        logger.info(">>>>>>>>>Time zone:{}", gmtTimeZone);
        String date = "2022-12-15 00:00:00";
        timeCalWithZone(timeFormat, areaCityTimeZone, date);
        timeCalWithZone(timeFormat, gmtTimeZone, date);
    }

    /**
     * 根据时区计算时间戳.
     *
     * @param timeFormat 时间格式
     * @param timeZone   时区
     * @param date       待转换的时间
     * @return 指定时区的时间戳
     */
    public long timeCalWithZone(String timeFormat, TimeZone timeZone, String date) {
        SimpleDateFormat s = new SimpleDateFormat(timeFormat);
        s.setTimeZone(timeZone);
        try {
            Date d = s.parse(date);
            logger.info(">>>>>>>>>>With zone time:{}, timestamp:{}", d, d.getTime());
            return d.getTime();
        } catch (ParseException var4) {
            throw new RuntimeException(var4);
        }
    }
}

3.2 处理UTC

当转换的时间包含的时区不是系统时区,
通过LocalDateTime可指定时间的当前时区,
然后,在指定目标时区,计算最终的时间,样例如下。

package com.monkey.java_study.functiontest;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时区处理.
 *
 * @author xindaqi
 * @since 2022-11-01 16:04
 */
public class TimeTest {

    private static final Logger logger = LoggerFactory.getLogger(TimeTest.class);

    @Test
    public void testZoneWithLocalDateTime() {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        String date = "2022-12-15 00:00:00";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        // 零时区
        ZoneOffset zeroZone = ZoneOffset.ofTotalSeconds(0);
        ZoneOffset currentZone = OffsetDateTime.now().getOffset();
        String time = calWithZoneInLocalDateTime(date, dtf, currentZone, "UTC+05:00");
        logger.info(">>>>>>>>>Time:{}", time);
    }

    /**
     * LocalDateTime根据时区计算时间.
     *
     * @param date        日期
     * @param dtf         日期格式器,如Formatter(yyyy-MM-dd HH:mm:ss)
     * @param currentZone 当前时区,传入的日期所在的时区
     * @param timeZone    目标时区,传入的日期转换到的时区
     * @return dtf格式的日期
     */
    public String calWithZoneInLocalDateTime(String date, DateTimeFormatter dtf, ZoneOffset currentZone, String timeZone) {
        LocalDateTime localDateTime = LocalDateTime.parse(date, dtf);
        long timestamp = localDateTime.atZone(currentZone).withZoneSameInstant(ZoneId.of(timeZone)).toInstant().toEpochMilli();
        logger.info(">>>>>>>>Timestamp:{}", timestamp);
        return localDateTime.atZone(currentZone).withZoneSameInstant(ZoneId.of(timeZone)).format(dtf);
    }
}

4 小结

(1)全球分为24个时区,其中,包括零时区,东12区和西12区;
(2)计时方式:UTC和GMT,精度有差异;
(3)SimpleDateFormat处理时区,无法指定时间所在的时区,指定使用系统时区;
(4)SimpleDateFormat处理UTC时区时,需要转换为GMT;
(5)LocalDateTime可同时指定时间所在的时区和目标时区,可直接转换UTC时区时间。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值