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

Android中如何解析2022-03-12T18:15:30+01:00[Europe/Paris]格式的日期?

解析带时区ID的日期时间格式

你遇到的问题是因为SimpleDateFormat属于Java旧版日期时间API,它不支持V(时区ID)这个模式字符,而且你的日期字符串里没有毫秒部分,格式串中的.SSS也是多余的。

推荐使用Java 8及以后版本引入的java.time包下的新API来处理,这个API更安全、功能更完善,原生支持时区ID的解析。具体实现如下:

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

public class DateTimeParser {
    public static void main(String[] args) {
        String dateStr = "2022-03-12T18:15:30+01:00[Europe/Paris]";
        // 定义匹配格式:XXX处理偏移量,'['V']'匹配带括号的时区ID
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX'['V']'");
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateStr, formatter);
        System.out.println(zonedDateTime);
    }
}

解析后得到的ZonedDateTime对象包含了完整的日期、时间、偏移量和时区信息,后续可以根据需求进行格式化或其他操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 14:54:30