LocalDateTime 时区转换,转指定时区

需求

服务器在国外,数据传到国内,LocalDateTime获取到的时间是国外时间,需要转换成国内时间,又或者服务器在国内,数据传到国外,需要转换成国外的时间。

直接上代码

/**
 * 获取指定时区的时间
 * @author Riyue-陈奇
 * @Date 2021-04-29 21:39
 * @param localDateTime
 * @param zoneIdEnum
 * @return java.time.LocalDateTime
 **/
public static LocalDateTime getCustomizeLocalDateTime(LocalDateTime localDateTime, ZoneIdEnum zoneIdEnum){
    // 当前系统时区
    ZoneId currentZone = getZone();
    // 新时区
    ZoneId newZone = ZoneId.of(zoneIdEnum.getZoneIdName());
    // 时区转换
    return localDateTime.atZone(currentZone).withZoneSameInstant(newZone).toLocalDateTime();
}

/**
 * 获取默认时区
 * @Author Riyue-陈奇
 * @Date 13:45 2020/12/18
 * @return java.time.ZoneOffset
 **/
public static ZoneOffset getZone(){
    return OffsetDateTime.now().getOffset();
}

常用时区枚举

public enum ZoneIdEnum {

    /**
     * "Australia/Darwin","澳洲/达尔文"
     */
    ACT("Australia/Darwin", "澳洲/达尔文"),

    /**
     * "Australia/Sydney","澳洲/悉尼"
     */
    AET("Australia/Sydney", "澳洲/悉尼"),

    /**
     * "America/Argentina/Buenos_Aires","美洲/阿根廷/布宜诺斯艾利斯"
     */
    AGT("America/Argentina/Buenos_Aires", "美洲/阿根廷/布宜诺斯艾利斯"),

    /**
     * "Africa/Cairo","非洲/开罗"
     */
    ART("Africa/Cairo", "非洲/开罗"),

    /**
     * "America/Anchorage","美洲/安克雷奇"
     */
    AST("America/Anchorage", "美洲/安克雷奇"),

    /**
     * "America/Sao_Paulo","美洲/圣保罗"
     */
    BET("America/Sao_Paulo", "美洲/圣保罗"),

    /**
     * "Asia/Dhaka","亚洲/达卡"
     */
    BST("Asia/Dhaka", "亚洲/达卡"),

    /**
     * "Africa/Harare","非洲/哈拉雷"
     */
    CAT("Africa/Harare", "非洲/哈拉雷"),

    /**
     * "America/St_Johns","美洲/圣约翰"
     */
    CNT("America/St_Johns", "美洲/圣约翰"),

    /**
     * "America/Chicago","美洲/芝加哥"
     */
    CST("America/Chicago", "美洲/芝加哥"),

    /**
     * "Asia/Shanghai","亚洲/上海"
     */
    CTT("Asia/Shanghai", "亚洲/上海"),

    /**
     * "Africa/Addis_Ababa","非洲/亚的斯亚贝巴"
     */
    EAT("Africa/Addis_Ababa", "非洲/亚的斯亚贝巴"),

    /**
     * "Europe/Paris","欧洲/巴黎"
     */
    ECT("Europe/Paris", "欧洲/巴黎"),

    /**
     * "America/Indiana/Indianapolis","美洲/印第安纳州/印第安纳波利斯"
     */
    IET("America/Indiana/Indianapolis", "美洲/印第安纳州/印第安纳波利斯"),

    /**
     * "Asia/Kolkata","亚洲/加尔各答"
     */
    IST("Asia/Kolkata", "亚洲/加尔各答"),

    /**
     * "Asia/Tokyo","亚洲/东京"
     */
    JST("Asia/Tokyo", "亚洲/东京"),

    /**
     * "Pacific/Apia","太平洋/阿皮亚"
     */
    MIT("Pacific/Apia", "太平洋/阿皮亚"),

    /**
     * "Asia/Yerevan","亚洲/埃里温"
     */
    NET("Asia/Yerevan", "亚洲/埃里温"),

    /**
     * "Pacific/Auckland","太平洋/奥克兰"
     */
    NST("Pacific/Auckland", "太平洋/奥克兰"),

    /**
     * "Asia/Karachi","亚洲/卡拉奇"
     */
    PLT("Asia/Karachi", "亚洲/卡拉奇"),

    /**
     * "America/Phoenix","美洲/凤凰城"
     */
    PNT("America/Phoenix", "美洲/凤凰城"),

    /**
     * "America/Puerto_Rico","美洲/波多黎各"
     */
    PRT("America/Puerto_Rico", "美洲/波多黎各"),

    /**
     * "America/Los_Angeles","美洲/洛杉矶"
     */
    PST("America/Los_Angeles", "美洲/洛杉矶"),

    /**
     * "Pacific/Guadalcanal","太平洋/瓜达尔卡纳尔岛"
     */
    SST("Pacific/Guadalcanal", "太平洋/瓜达尔卡纳尔岛"),

    /**
     * "Asia/Ho_Chi_Minh","亚洲/胡志明市"
     */
    VST("Asia/Ho_Chi_Minh", "亚洲/胡志明市"),

    /**
     * "-05:00","东部标准时间"(纽约、华盛顿)
     */
    EST("-05:00", "东部标准时间"),

    /**
     * "-07:00","山地标准时间"
     */
    MST("-07:00", "山地标准时间"),

    /**
     * "-10:00","夏威夷-阿留申标准时区"
     */
    HST("-10:00", "夏威夷-阿留申标准时区"),;

    private final String zoneIdName;
    private final String zoneIdNameCn;

    public String getZoneIdName() {
        return zoneIdName;
    }

    public String getZoneIdNameCn() {
        return zoneIdNameCn;
    }

    ZoneIdEnum(String zoneIdName, String zoneIdNameCn) {
        this.zoneIdName = zoneIdName;
        this.zoneIdNameCn = zoneIdNameCn;
    }
}

参考:http://www.voidcn.com/article/p-srhlcsnx-bwa.html

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要进行 `LocalDateTime` 的时区转换,你可以使用 `ZonedDateTime` 类。下面是一个示例代码,将一个 `LocalDateTime` 对象从一个时区转换为另一个时区: ```java import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class TimeZoneConversionExample { public static void main(String[] args) { // 创建一个 LocalDateTime 对象 LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 12, 0); // 定义原始时区和目标时区 ZoneId sourceZone = ZoneId.of("Asia/Shanghai"); ZoneId targetZone = ZoneId.of("America/New_York"); // 将 LocalDateTime 转换为 ZonedDateTime,并指定原始时区 ZonedDateTime sourceZonedDateTime = localDateTime.atZone(sourceZone); // 将 ZonedDateTime 转换为目标时区 ZonedDateTime targetZonedDateTime = sourceZonedDateTime.withZoneSameInstant(targetZone); // 获取转换后的 LocalDateTime 对象 LocalDateTime targetLocalDateTime = targetZonedDateTime.toLocalDateTime(); System.out.println("原始 LocalDateTime: " + localDateTime); System.out.println("目标 LocalDateTime: " + targetLocalDateTime); } } ``` 在上述示例中,我们首先创建了一个 `LocalDateTime` 对象 `localDateTime`,然后定义了原始时区 `sourceZone` 和目标时区 `targetZone`。接下来,我们将 `localDateTime` 转换为 `ZonedDateTime` 对象,指定了原始时区。然后,我们使用 `withZoneSameInstant()` 方法将其转换为目标时区的 `ZonedDateTime` 对象。最后,我们使用 `toLocalDateTime()` 方法获取转换后的 `LocalDateTime` 对象 `targetLocalDateTime`。 你可以根据自己的需求修改示例

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值