From f13b677449e6507b08f6cc790c0e5832c0673720 Mon Sep 17 00:00:00 2001 From: Umut Ay Bora Date: Fri, 20 Jun 2025 17:46:39 +0200 Subject: [PATCH 1/3] Fixed versioning and removed redundant method --- pom.xml | 2 +- src/main/java/utils/DateUtilities.java | 105 +++++++++++++++++++------ src/test/java/AppTest.java | 2 +- 3 files changed, 82 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index b9fc919..ed7b332 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.umutayb Utilities - 1.7.4 + 1.7.5 jar Java-Utilities diff --git a/src/main/java/utils/DateUtilities.java b/src/main/java/utils/DateUtilities.java index 14d8c6a..f93a9f3 100644 --- a/src/main/java/utils/DateUtilities.java +++ b/src/main/java/utils/DateUtilities.java @@ -223,26 +223,6 @@ public static String getCurrentDate(ZoneIds zoneId) { return dtf.format(now); } - /** - * Formats a date string from a specified input format to a desired output format. - * - * @param input The date string to format. - * @param inputFormat The format of the input date string (e.g., "yyyy-MM-dd"). - * @param outputFormat The desired format of the output date string (e.g., "MM/dd/yyyy"). - * @return The formatted date string. - * @throws RuntimeException If the input date string cannot be parsed according to the specified input format. - * The exception is a `RuntimeException` wrapping the original `ParseException`. - */ - public static String fixDateFormat(String input, String inputFormat, String outputFormat) { - try { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(inputFormat); - Date date = simpleDateFormat.parse(input); - SimpleDateFormat outputSimpleDateFormat = new SimpleDateFormat(outputFormat); - return outputSimpleDateFormat.format(date); - } - catch (ParseException exception) {throw new RuntimeException(exception);} - } - /** * Formats a date string from an automatically detected input format to a * user-specified output format. @@ -252,14 +232,89 @@ public static String fixDateFormat(String input, String inputFormat, String outp * @return The formatted date string, or the original input string if the * input format cannot be detected. */ - public static String fixDateFormat(String input, String outputFormat) { + public static String reformatDateString(String input, String outputFormat) { String[] SUPPORTED_INPUT_FORMATS = { - "yyyy-M-dd", "yyyy-MM-dd", "M/d/yyyy", "MM/d/yyyy", "yyyy/M/d", "yyyy/MM/d", - "M-d-yyyy", "MM-d-yyyy", "yyyy-M-d", "yyyy-MM-d" + "yyyy-M-dd", // Original + "yyyy-MM-dd", // Original + "M/d/yyyy", // Original + "MM/d/yyyy", // Original + "yyyy/M/d", // Original + "yyyy/MM/d", // Original + "M-d-yyyy", // Original + "MM-d-yyyy", // Original + "yyyy-M-d", // Original + "yyyy-MM-d", // Original + + // Variations with different separators + "yyyy.M.dd", + "yyyy.MM.dd", + "M.d.yyyy", + "MM.d.yyyy", + "yyyy.M.d", + "yyyy.MM.d", + "M-d-yyyy", + "MM-d-yyyy", + + // Single digit month/day + "y-M-d", + "y-MM-d", + "M/d/yy", + "MM/d/yy", + "y/M/d", + "y/MM/d", + "M-d-yy", + "MM-d-yy", + + //No separators + "yyyyMdd", + "yyMdd", + "yyyyMd", + "yyMd", + "Myyyy", + "Mddyy", + "Mdyyyy", + + //Common International Formats + "dd/M/yyyy", // Common in Europe + "dd/MM/yyyy", // Common in Europe + "dd-M-yyyy", + "dd-MM-yyyy", + "d/M/yyyy", // Single-digit day + "d/MM/yyyy", // Single-digit day + + //More variations + "yyyyMdd", + "yyMdd", + "yyyyMd", + "yyMd", + "Mdyyyy", + "Mddyy", + "Myyyy", + + // Short year format + "M/d/yy", + "MM/d/yy", + "M-d-yy", + "MM-d-yy", + "dd/M/yy", + "dd/MM/yy", + + //More separators + "M/d/yyyy", + "MM/d/yyyy", + "M-d-yyyy", + "MM-d-yyyy", + "d/M/yyyy", + "d/MM/yyyy", + + //Edge cases/less common + "yyyyMMdd", //No Separators + "yyMMdd", //No Separators }; for (String inputFormat : SUPPORTED_INPUT_FORMATS) { - try {return fixDateFormat(input, inputFormat, outputFormat);} - catch (Exception ignored) {} + String formattedDate = reformatDateString(input, inputFormat, outputFormat); + if (formattedDate == null) continue; + return formattedDate; } return input; } diff --git a/src/test/java/AppTest.java b/src/test/java/AppTest.java index 81dcaca..ca73ec5 100644 --- a/src/test/java/AppTest.java +++ b/src/test/java/AppTest.java @@ -347,7 +347,7 @@ public void dateFormatTest() { Assert.assertEquals( "Fixed date format did not match the expected one!", expectedDate, - DateUtilities.fixDateFormat(date, "yyyy-MM-dd") + DateUtilities.reformatDateString(date, "yyyy-MM-dd") ); printer.success("The dateFormatTest() test pass!"); } From abf7f272e2d325dfeb28716e72f2f18714c49a79 Mon Sep 17 00:00:00 2001 From: Umut Ay Bora Date: Fri, 20 Jun 2025 17:48:17 +0200 Subject: [PATCH 2/3] Update DateUtilities.java --- src/main/java/utils/DateUtilities.java | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/utils/DateUtilities.java b/src/main/java/utils/DateUtilities.java index f93a9f3..baa634b 100644 --- a/src/main/java/utils/DateUtilities.java +++ b/src/main/java/utils/DateUtilities.java @@ -234,16 +234,17 @@ public static String getCurrentDate(ZoneIds zoneId) { */ public static String reformatDateString(String input, String outputFormat) { String[] SUPPORTED_INPUT_FORMATS = { - "yyyy-M-dd", // Original - "yyyy-MM-dd", // Original - "M/d/yyyy", // Original - "MM/d/yyyy", // Original - "yyyy/M/d", // Original - "yyyy/MM/d", // Original - "M-d-yyyy", // Original - "MM-d-yyyy", // Original - "yyyy-M-d", // Original - "yyyy-MM-d", // Original + // Standard formats + "yyyy-M-dd", + "yyyy-MM-dd", + "M/d/yyyy", + "MM/d/yyyy", + "yyyy/M/d", + "yyyy/MM/d", + "M-d-yyyy", + "MM-d-yyyy", + "yyyy-M-d", + "yyyy-MM-d", // Variations with different separators "yyyy.M.dd", From e2a22fa82e615c26eb209e761ab771da6eb4b887 Mon Sep 17 00:00:00 2001 From: Umut Ay Bora Date: Fri, 20 Jun 2025 17:49:28 +0200 Subject: [PATCH 3/3] Update DateUtilities.java --- src/main/java/utils/DateUtilities.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/utils/DateUtilities.java b/src/main/java/utils/DateUtilities.java index baa634b..2345d3a 100644 --- a/src/main/java/utils/DateUtilities.java +++ b/src/main/java/utils/DateUtilities.java @@ -276,12 +276,12 @@ public static String reformatDateString(String input, String outputFormat) { "Mdyyyy", //Common International Formats - "dd/M/yyyy", // Common in Europe - "dd/MM/yyyy", // Common in Europe + "dd/M/yyyy", + "dd/MM/yyyy", "dd-M-yyyy", "dd-MM-yyyy", - "d/M/yyyy", // Single-digit day - "d/MM/yyyy", // Single-digit day + "d/M/yyyy", + "d/MM/yyyy", //More variations "yyyyMdd", @@ -308,9 +308,9 @@ public static String reformatDateString(String input, String outputFormat) { "d/M/yyyy", "d/MM/yyyy", - //Edge cases/less common - "yyyyMMdd", //No Separators - "yyMMdd", //No Separators + //No Separators + "yyyyMMdd", + "yyMMdd", }; for (String inputFormat : SUPPORTED_INPUT_FORMATS) { String formattedDate = reformatDateString(input, inputFormat, outputFormat);