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

求助:Java中统一转换多种日期格式的实现方案

Java多格式日期转换异常修复方案

需要统一转换以下三种日期格式:

  • 2023-12-10
  • 2023-12-10T00:00:00.000
  • 2023-12-10T00:00:00.000000

原代码及异常

原代码存在拼写、语法错误以及格式匹配问题,运行时抛出:

java.time.format.DateTimeParseException: Text '2023-12-10T00:00:00.000' could not be parsed at index 20

原代码:

public statc void main(String[] a) {
  convert("2023-12-10");
  convert("2023-12-10T00:00:00.000");
  convert("2023-12-10T00:00:00.000000");
}

private static void convert(String date){
  try {
    System.out.println(LocalDate.parse(date));
  } catch (Exception e) {
    System.out.println(LocalDate.parse(date,DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS"));
  }
}

错误原因

  1. 拼写错误:statc应为static;
  2. 语法错误:catch块中LocalDate.parse调用缺少闭合括号;
  3. 格式匹配问题:SSSSSSSSS模式要求毫秒部分必须是9位,但输入的日期只有3位或6位毫秒,不符合强制长度要求,导致解析失败。

修复方案

方案一:使用DateTimeFormatterBuilder构建灵活解析器

通过DateTimeFormatterBuilder定义可选的时间和毫秒部分,支持1-9位毫秒的解析:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

public class DateConverter {
    public static void main(String[] args) {
        convert("2023-12-10");
        convert("2023-12-10T00:00:00.000");
        convert("2023-12-10T00:00:00.000000");
    }

    private static void convert(String date) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                // 基础日期格式
                .appendPattern("yyyy-MM-dd")
                // 可选的时间部分(T+时分秒)
                .optionalStart()
                .appendPattern("'T'HH:mm:ss")
                // 可选的毫秒部分,支持1-9位
                .optionalStart()
                .appendFraction(ChronoField.NANO_OF_SECOND, 1, 9, true)
                .optionalEnd()
                .optionalEnd()
                .toFormatter();

        LocalDate localDate = LocalDate.parse(date, formatter);
        System.out.println(localDate);
    }
}

方案二:定义多个格式依次尝试

预先定义所有支持的格式,循环尝试解析,直到成功:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

public class DateConverter {
    // 定义所有支持的日期格式
    private static final List<DateTimeFormatter> SUPPORTED_FORMATTERS = Arrays.asList(
            DateTimeFormatter.ISO_LOCAL_DATE, // 匹配2023-12-10
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"), // 匹配3位毫秒
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS") // 匹配6位毫秒
    );

    public static void main(String[] args) {
        convert("2023-12-10");
        convert("2023-12-10T00:00:00.000");
        convert("2023-12-10T00:00:00.000000");
    }

    private static void convert(String date) {
        for (DateTimeFormatter formatter : SUPPORTED_FORMATTERS) {
            try {
                LocalDate localDate = LocalDate.parse(date, formatter);
                System.out.println(localDate);
                return;
            } catch (Exception ignored) {
                // 尝试下一个格式
            }
        }
        throw new IllegalArgumentException("无法解析日期字符串: " + date);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 17:23:33