Java中如何解析dd-m-y格式日期?SimpleDateFormat使用问题求助
Hey, I get it—parsing date formats with abbreviated months like 12-oct-14 can be tricky, especially when dealing with locale-specific quirks. Let’s break down how to solve this with both legacy and modern Java tools, plus some handy alternatives that make this process smoother.
1. 使用SimpleDateFormat(旧版本兼容方案)
If your project is stuck on Java 8 or earlier, SimpleDateFormat can get the job done—but you need to nail two key details: the correct pattern and locale.
示例代码
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class LegacyDateParser { public static void main(String[] args) { String inputDate = "12-oct-14"; // 核心:用MMM匹配月份缩写,指定英文Locale确保解析生效 SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH); try { Date parsedDate = formatter.parse(inputDate); System.out.println("解析结果:" + parsedDate); // 可选:转成你需要的输出格式 SimpleDateFormat outputFormatter = new SimpleDateFormat("yyyy-MM-dd"); System.out.println("格式化后:" + outputFormatter.format(parsedDate)); } catch (ParseException e) { e.printStackTrace(); } } }
关键注意点
dd-MMM-yy模式:dd对应两位日期,MMM匹配英文月份缩写(如oct、jan),yy匹配两位年份。- 必须指定
Locale.ENGLISH:如果你的系统默认Locale不是英文,解析器会识别不了oct这类缩写,直接抛出异常。 - 两位年份解析:Java会自动把
yy映射到1970-2069区间,所以14会被解析为2014,70则对应1970。
2. 使用Java 8+ DateTimeFormatter(推荐方案)
For Java 8 and above, ditch the thread-unsafe SimpleDateFormat and use the modern java.time API. It’s immutable, thread-safe, and has a cleaner design.
示例代码
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class ModernDateParser { public static void main(String[] args) { String inputDate = "12-oct-14"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yy", Locale.ENGLISH); // 直接解析为LocalDate(仅日期,无时间) LocalDate parsedDate = LocalDate.parse(inputDate, formatter); System.out.println("解析结果:" + parsedDate); // 轻松转换为其他格式 System.out.println("yyyy-MM-dd格式:" + parsedDate.format(DateTimeFormatter.ISO_LOCAL_DATE)); } }
为什么推荐这个?
- 线程安全:可以在多线程环境下放心使用,不用像
SimpleDateFormat那样每次都新建实例。 - 类型安全:
LocalDate专门处理日期,避免了旧API中Date类混同日期和时间的混乱。 - 更清晰的错误处理:解析失败时的异常信息更直观,便于调试。
3. 第三方工具库
If you want to skip writing boilerplate code, these libraries can simplify date parsing:
- Apache Commons Lang:
DateUtils类支持一次性解析多种格式,非常适合需要兼容多种日期输入的场景。import org.apache.commons.lang3.time.DateUtils; import java.text.ParseException; import java.util.Date; import java.util.Locale; public class CommonsDateParser { public static void main(String[] args) { String inputDate = "12-oct-14"; try { Date parsedDate = DateUtils.parseDate(inputDate, Locale.ENGLISH, "dd-MMM-yy"); System.out.println("解析结果:" + parsedDate); } catch (ParseException e) { e.printStackTrace(); } } } - Joda-Time:虽然已经被Java 8的
java.time取代,但一些旧项目仍在使用它。它的DateTimeFormat类同样能轻松处理这种格式。
总结
- Java 8+:优先用
DateTimeFormatter+LocalDate,安全又省心。 - Java 7及以下:用
SimpleDateFormat,务必记得指定Locale.ENGLISH。 - 追求便捷:可以用Apache Commons Lang的
DateUtils减少重复代码。
内容的提问来源于stack exchange,提问作者kunal

