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

如何在JDK11中将多种日期格式转换为dd-mm-yyyy格式?

解决JDK11中多格式日期转换为dd-MM-yyyy的问题

首先纠正一个细节:你提到的目标格式dd-mm-yyyy里的mm代表分钟,日期的月份应该用大写的MM,所以正确的目标格式是dd-MM-yyyy。

在JDK11中,直接用自带的java.time包(JDK8引入的日期时间API,完全兼容JDK11)就能实现,不需要额外引入第三方库。核心思路是:定义所有需要支持的日期格式,逐个尝试解析输入字符串,成功后统一格式化为目标样式。

具体实现步骤

  1. 定义所有支持的日期格式器
    针对你列出的5种日期格式,对应创建DateTimeFormatter实例,注意处理时区、英文月份缩写的Locale问题:

    • 2023-05-09T05:55:07Z:对应格式yyyy-MM-dd'T'HH:mm:ssX(X处理Z时区标识)
    • 2023-04-30:对应格式yyyy-MM-dd
    • 08-05-2023:对应格式dd-MM-yyyy
    • 2023-05-09 00:00:00:对应格式yyyy-MM-dd HH:mm:ss
    • 31-Dec-2021:对应格式dd-MMM-yyyy,需要指定Locale.ENGLISH确保英文月份缩写能被正确解析
  2. 编写转换工具方法
    遍历格式器列表,尝试解析输入字符串,一旦解析成功,就将日期转换为LocalDate(忽略时间部分,因为目标格式只需要年月日),最后格式化为dd-MM-yyyy。

完整代码示例

import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class DateConverter {
    // 定义所有支持的日期格式器
    private static final List<DateTimeFormatter> FORMATTERS = Arrays.asList(
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd"),
            DateTimeFormatter.ofPattern("dd-MM-yyyy"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
            DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH)
    );

    // 目标格式器
    private static final DateTimeFormatter TARGET_FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy");

    public static String convertToDdMmYyyy(String inputDate) {
        if (inputDate == null || inputDate.isBlank()) {
            throw new IllegalArgumentException("输入日期不能为空");
        }

        for (DateTimeFormatter formatter : FORMATTERS) {
            try {
                // 处理带Z的时区格式
                if (inputDate.contains("Z")) {
                    ZonedDateTime zonedDateTime = ZonedDateTime.parse(inputDate, formatter);
                    return zonedDateTime.toLocalDate().format(TARGET_FORMATTER);
                } else {
                    // 其他格式直接解析为LocalDate
                    LocalDate localDate = LocalDate.parse(inputDate, formatter);
                    return localDate.format(TARGET_FORMATTER);
                }
            } catch (DateTimeParseException e) {
                // 当前格式解析失败,尝试下一个
                continue;
            }
        }

        throw new IllegalArgumentException("不支持的日期格式:" + inputDate);
    }

    // 测试示例
    public static void main(String[] args) {
        List<String> testDates = Arrays.asList(
                "2023-05-09T05:55:07Z",
                "2023-04-30",
                "08-05-2023",
                "2023-05-09 00:00:00",
                "31-Dec-2021"
        );

        for (String date : testDates) {
            try {
                String result = convertToDdMmYyyy(date);
                System.out.printf("原始日期:%s → 转换后:%s%n", date, result);
            } catch (Exception e) {
                System.out.printf("处理日期%s失败:%s%n", date, e.getMessage());
            }
        }
    }
}

代码说明

  • 针对带Z的ISO格式,用ZonedDateTime解析后转成LocalDate,自动忽略时区和时间部分
  • 英文月份格式指定Locale.ENGLISH,避免在非英文系统环境下解析失败
  • 遍历格式器时捕获DateTimeParseException,失败则尝试下一个格式
  • 最后如果所有格式都匹配失败,抛出明确的异常信息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 21:25:27