You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java将UTC日期字符串转换为BST时区指定格式的日期

Java 实现指定日期字符串转BST时区格式方案

推荐直接使用Java 8及以上内置的java.time包实现,无需引入第三方依赖,线程安全且自动适配夏令时规则,完全满足你的转换需求。

完整可运行代码

import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class BstDateConvert {
    public static void main(String[] args) {
        // 原始输入日期字符串
        String originDate = "2021-08-21T17:36:51.000+00:00";
        // 定义标准伦敦时区(自动适配夏令时,8月对应BST+1时区)
        ZoneId bstZone = ZoneId.of("Europe/London");
        // 定义目标输出格式
        DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm a z'(+1)'");

        // 1. 解析原始ISO标准格式的日期
        ZonedDateTime utcTime = ZonedDateTime.parse(originDate);
        // 2. 转换为BST时区时间
        ZonedDateTime bstTime = utcTime.withZoneSameInstant(bstZone);
        // 3. 按指定格式输出
        String convertResult = bstTime.format(targetFormatter);

        System.out.println(convertResult);
        // 输出结果:2021-08-21 06:36 PM BST(+1)
    }
}

实现说明

  • 时区ID不要直接填写BST,该缩写存在全球歧义(多个地区时区缩写同为BST),使用Europe/London标准时区ID会自动匹配英国夏令时规则,8月处于夏令时阶段,偏移量自动为+1,完全匹配需求。
  • 格式串说明:hh对应12小时制小时数、a对应AM/PM标识、z对应时区缩写。
  • 若使用Java 8以下版本,可引入ThreeTen-Backport依赖适配java.time相关API,不推荐使用旧的SimpleDateFormat实现,存在线程安全和时区适配误差问题。

内容的提问来源于stack exchange,提问作者David Ishak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 23:42:03