Use Java with Apache Commons Lang3 and Natty libraries to achieve flexible and efficient date parsing and formatting.
1. Background
Unify dates in different formats into one format. The date format may be of the following types:
- Standard format: 2024-02-28, 14/05/2022, May 6, 2002
- Non-English Month Abbreviation: 02 NIS 2018, 26 AGO 2018 (Spanish)
- Natural language: next Monday, two days ago
2. Dependence introduction
The version used is jdk8. Use the following two date parsing libraries:
1. Apache Commons Lang3
Apache Commons Lang3 provides a wealth of tools that can strictly parse various standard date formats.
<dependency> <groupId></groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>
2. Natty
Natty is a Java library specifically used to parse natural language dates, supporting various date expressions described in English.
<dependency> <groupId></groupId> <artifactId>natty</artifactId> <version>0.13</version> </dependency>
3. Core implementation code
Define a tool class and provide a unified date resolution method:
Complete code example:
import .; import ; import .; import ; import ; import .*; /*** * @title * @author shijiangyong * @date 2025/5/14 14:18 **/ public class SmartDateParser { // Date format allowed to be parsed private static final String[] DATE_PATTERNS = { "yyyy-MM-dd", "yyyy/M/d", "dd/MM/yyyy", "Yyyyy's M-D Day", "dd MMM yyyy", "MMM dd yyyy", "dd MM yyyy", "yyyyMMdd", "", "yyyyYearMMmoonddday", "d MMM yyyy", "EEE, dd MMM yyyy" }; // Handle non-standard month abbreviations (mainly for Spanish, French, etc.) private static final Map<String, String> MONTH_CORRECTIONS = new HashMap<>(); static { MONTH_CORRECTIONS.put("NIS", "APR"); // It may be April MONTH_CORRECTIONS.put("ABR", "APR"); // Spanish April MONTH_CORRECTIONS.put("AGO", "AUG"); // Spanish August } /** * Parses the date and convert it to yyyy-M-d format */ public static String parseDate(String input) { if (input == null || ().isEmpty()) { return "Invalid Date"; } input = ().replaceAll("[,,]", ""); // Revise the abbreviation of the month for (<String, String> entry : MONTH_CORRECTIONS.entrySet()) { if ((())) { input = ().replace((), ()); } } // 1. Clearly give priority to the format for (String pattern : DATE_PATTERNS) { try { SimpleDateFormat sdf = new SimpleDateFormat(pattern, ); // Strict verification date (false); Date date = (input); return formatToStandard(date); } catch (ParseException ignored) {} } // 1. Use Apache Commons Lang3 to parse try { Date date = (input, , DATE_PATTERNS); return formatToStandard(date); } catch (ParseException ignored) {} // 2. Use Natty parsing (for `next Monday`, `19 AUG 2019`) try { Parser parser = new Parser(); List<> groups = (input); if (!()) { List<Date> dates = (0).getDates(); if (!()) { return formatToStandard((0)); } } } catch (Exception ignored) {} return "Unrecognized: " + input; } /** * The unified conversion date is yyyy-MM-dd format */ private static String formatToStandard(Date date) { Calendar cal = (); (date); int year = (); int month = () + 1; int day = (Calendar.DAY_OF_MONTH); return ("%04d-%02d-%02d", year, month, day); } public static void main(String[] args) { .().setLevel(); List<String> testDates = ( "02 NIS 2018", "2028-4-219", "19 AUG 2019", "2019-8-19", "May 6, 2002", "2005/02/03", "03 SEP 1985", "14/05/2022", "20 FEB 1991", "26 AGO 2018", "08 ABR 1975", "01 09 1988", "next Monday", "yesterday", "two days ago", "2024.02.28", "Wed, 19 Aug 2019" ); for (String dateStr : testDates) { ("Input: " + dateStr + " → Analysis: " + parseDate(dateStr)); } } }
Analysis results:
Input: 02 NIS 2018 → Analysis: 2018-04-02
Input: 2028-4-219 → Analysis: 2028-04-21
Input: 19 AUG 2019 → Analysis: 2019-08-19
Input: 2019-8-19 → Analysis: 2019-08-19
Enter: May 6, 2002 → Analysis: 2002-05-06
Input: 2005/02/03 → Analysis: 2005-02-03
Input: 03 SEP 1985 → Analysis: 1985-09-03
Input: 14/05/2022 → Analysis: 2022-05-14
Input: 20 FEB 1991 → Analysis: 1991-02-20
Input: 26 AGO 2018 → Analysis: 2018-08-26
Input: 08 ABR 1975 → Analysis: 1975-04-08
Input: 01 09 1988 → Analysis: 1988-09-01
Input: next Monday → Analysis: 2025-05-19
Input: yesterday → Analysis: 2025-05-13
Enter: two days ago → Analysis: 2025-05-12
Input: 2024.02.28 → Analysis: 2024-02-28
Input: Wed, 19 Aug 2019 → Analysis: 2019-08-19
The required date formats are parsed correctly.
This is the article about Java date parsing and formatting implementation code. For more related Java date parsing and formatting content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!