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

Java解析SQLite中"Apr 5, 2018"日期报错,如何提取年月格式

解决Java中SQLite日期解析异常并提取年月格式的问题

我来帮你搞定这个日期解析的麻烦——你遇到的Unparseable date异常,主要是因为日期格式模板不匹配,或者没有指定正确的Locale导致的。下面给你两种可行的解决方案,还有针对SQLite数据库场景的实际用法:

为什么会抛出异常?

  • 你的日期字符串是Apr 5, 2018,如果用了错误的格式模板(比如MM/dd/yyyy)去解析,肯定会失败。
  • 另一个常见原因是Locale问题:如果系统默认Locale不是英文,解析器会用本地语言去识别月份缩写,自然认不出Apr这种英文标识。

方案1:使用Java 8+推荐的新日期API(首选)

这个方案是现在的最佳实践,新API是线程安全的,设计更合理,不会踩旧API的坑。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

// 适配SQLite数据库查询场景的完整示例
public class SQLiteDateHandler {
    public static void main(String[] args) {
        try (Connection conn = /* 这里替换为你的数据库连接逻辑 */;
             PreparedStatement stmt = conn.prepareStatement("SELECT Bill_Date FROM your_bill_table");
             ResultSet rs = stmt.executeQuery()) {

            // 定义输入格式:完全匹配"Apr 5, 2018",必须指定英文Locale
            DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.US);
            // 定义输出格式:"Apr, 2018"
            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MMM, yyyy", Locale.US);

            while (rs.next()) {
                String billDateStr = rs.getString("Bill_Date");
                // 解析日期字符串
                LocalDate billDate = LocalDate.parse(billDateStr, inputFormatter);
                // 格式化为需要的年月格式
                String yearMonth = billDate.format(outputFormatter);
                System.out.println("提取的年月:" + yearMonth);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

方案2:使用旧的SimpleDateFormat(兼容Java 8之前版本)

如果你的项目还在使用Java 8之前的版本,可以用这个方案,但要注意SimpleDateFormat不是线程安全的,别在多线程环境下共享同一个实例。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class OldSQLiteDateHandler {
    public static void main(String[] args) {
        try (Connection conn = /* 这里替换为你的数据库连接逻辑 */;
             PreparedStatement stmt = conn.prepareStatement("SELECT Bill_Date FROM your_bill_table");
             ResultSet rs = stmt.executeQuery()) {

            // 输入格式,指定英文Locale避免解析失败
            SimpleDateFormat inputFormat = new SimpleDateFormat("MMM d, yyyy", Locale.US);
            // 输出格式
            SimpleDateFormat outputFormat = new SimpleDateFormat("MMM, yyyy", Locale.US);

            while (rs.next()) {
                String billDateStr = rs.getString("Bill_Date");
                try {
                    // 解析原始日期字符串
                    java.util.Date billDate = inputFormat.parse(billDateStr);
                    // 格式化为目标年月格式
                    String yearMonth = outputFormat.format(billDate);
                    System.out.println("提取的年月:" + yearMonth);
                } catch (ParseException e) {
                    System.err.println("解析日期失败:" + billDateStr);
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

关键注意点

  • 必须指定Locale.US:因为你的日期月份是英文缩写,不指定的话,系统默认Locale如果是中文,会尝试解析中文月份,直接导致失败。
  • 格式模板要完全匹配:MMM对应英文月份缩写(比如Apr),d对应1位或2位的日期(比如5或05),yyyy对应4位年份(比如2018)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 10:10:29