From 83ff17a949e368c652e57f65567c8eefe1c625fe Mon Sep 17 00:00:00 2001 From: Umut Ay Bora Date: Fri, 20 Jun 2025 17:17:48 +0200 Subject: [PATCH 1/3] Update DateUtilities.java --- src/main/java/utils/DateUtilities.java | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/main/java/utils/DateUtilities.java b/src/main/java/utils/DateUtilities.java index 70f6b95..5d2be0d 100644 --- a/src/main/java/utils/DateUtilities.java +++ b/src/main/java/utils/DateUtilities.java @@ -9,6 +9,8 @@ import java.time.temporal.ChronoUnit; import java.util.Calendar; import java.util.Date; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class DateUtilities { @@ -220,4 +222,56 @@ public static String getCurrentDate(ZoneIds zoneId) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); return dtf.format(now); } + + /** + * Fixes a date string assuming the input format is "yyyy-M-dd". + * + * @param input The date string to fix, expected in "yyyy-M-dd" format. + * @return The formatted date string in "yyyy-MM-dd" format, or the original input if it doesn't match. + * @throws IllegalArgumentException if input is null. + */ + public static String fixDateFormat(String input) { + // Regex to match the pattern "yyyy-M-dd" where M is a single digit month + Pattern pattern = Pattern.compile("(\\d{4})-(\\d)-(\\d{2})"); + Matcher matcher = pattern.matcher(input); + + return fixDateFormat(input, "(\\d{4})-(\\d)-(\\d{2})", "{1}-{2}-{3}"); + } + + /** + * Fixes a date string based on a provided input pattern and formats it according to an output pattern. + * + * @param input The date string to fix and format. + * @param inputPattern The regular expression pattern to match the input date format. Capture groups should be used to extract date components. + * @param outputPattern The desired output format string. Use `{1}`, `{2}`, etc. to refer to captured groups from the input pattern. + * @return The formatted date string if the input matches the pattern, otherwise the original input string. + * @throws IllegalArgumentException if input, inputPattern, or outputPattern is null. + */ + public static String fixDateFormat(String input, String inputPattern, String outputPattern) { + // Build the regex pattern dynamically + Pattern pattern = Pattern.compile(inputPattern); + Matcher matcher = pattern.matcher(input); + + if (matcher.matches()) { + // Extract groups based on the number of capture groups in the input pattern + String[] groups = new String[matcher.groupCount()]; + for (int i = 1; i <= matcher.groupCount(); i++) { + groups[i - 1] = matcher.group(i); + } + + // Format the output based on the output pattern. This is a simplified example + // and might need adjustments for more complex output formats. It assumes + // the output pattern uses numbered placeholders like {1}, {2}, etc. + String formattedDate = outputPattern; + for (int i = 0; i < groups.length; i++) { + formattedDate = formattedDate.replace("{" + (i + 1) + "}", groups[i]); + } + + return formattedDate; + + } else { + // If the input doesn't match the expected format, return it unchanged. + return input; + } + } } From b972ccd7efe72465db2d6c884e986b3c04a050de Mon Sep 17 00:00:00 2001 From: Umut Ay Bora Date: Fri, 20 Jun 2025 17:26:22 +0200 Subject: [PATCH 2/3] Update DateUtilities.java --- src/main/java/utils/DateUtilities.java | 76 ++++++++++++-------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/src/main/java/utils/DateUtilities.java b/src/main/java/utils/DateUtilities.java index 5d2be0d..245a1d4 100644 --- a/src/main/java/utils/DateUtilities.java +++ b/src/main/java/utils/DateUtilities.java @@ -224,54 +224,48 @@ public static String getCurrentDate(ZoneIds zoneId) { } /** - * Fixes a date string assuming the input format is "yyyy-M-dd". + * Formats a date string from an automatically detected input format to a + * user-specified output format. * - * @param input The date string to fix, expected in "yyyy-M-dd" format. - * @return The formatted date string in "yyyy-MM-dd" format, or the original input if it doesn't match. - * @throws IllegalArgumentException if input is null. + * @param input The date string to format. + * @param outputFormat The desired output format string (e.g., "yyyy-MM-dd"). + * @return The formatted date string, or the original input string if the + * input format cannot be detected. */ - public static String fixDateFormat(String input) { - // Regex to match the pattern "yyyy-M-dd" where M is a single digit month - Pattern pattern = Pattern.compile("(\\d{4})-(\\d)-(\\d{2})"); - Matcher matcher = pattern.matcher(input); - - return fixDateFormat(input, "(\\d{4})-(\\d)-(\\d{2})", "{1}-{2}-{3}"); + public static String fixDateFormat(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" + }; + for (String inputFormat : SUPPORTED_INPUT_FORMATS) { + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(inputFormat); + Date date = simpleDateFormat.parse(input); + SimpleDateFormat outputSimpleDateFormat = new SimpleDateFormat(outputFormat); + return outputSimpleDateFormat.format(date); + } + catch (ParseException ignored) {} + } + return input; } /** - * Fixes a date string based on a provided input pattern and formats it according to an output pattern. + * Formats a date string from a specified input format to a desired output format. * - * @param input The date string to fix and format. - * @param inputPattern The regular expression pattern to match the input date format. Capture groups should be used to extract date components. - * @param outputPattern The desired output format string. Use `{1}`, `{2}`, etc. to refer to captured groups from the input pattern. - * @return The formatted date string if the input matches the pattern, otherwise the original input string. - * @throws IllegalArgumentException if input, inputPattern, or outputPattern is null. + * @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 inputPattern, String outputPattern) { - // Build the regex pattern dynamically - Pattern pattern = Pattern.compile(inputPattern); - Matcher matcher = pattern.matcher(input); - - if (matcher.matches()) { - // Extract groups based on the number of capture groups in the input pattern - String[] groups = new String[matcher.groupCount()]; - for (int i = 1; i <= matcher.groupCount(); i++) { - groups[i - 1] = matcher.group(i); - } - - // Format the output based on the output pattern. This is a simplified example - // and might need adjustments for more complex output formats. It assumes - // the output pattern uses numbered placeholders like {1}, {2}, etc. - String formattedDate = outputPattern; - for (int i = 0; i < groups.length; i++) { - formattedDate = formattedDate.replace("{" + (i + 1) + "}", groups[i]); - } - - return formattedDate; - - } else { - // If the input doesn't match the expected format, return it unchanged. - return input; + 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);} } } From 4d0e7fd7121cd45d7a79a7db939e97bd86e6b585 Mon Sep 17 00:00:00 2001 From: Umut Ay Bora Date: Fri, 20 Jun 2025 17:33:17 +0200 Subject: [PATCH 3/3] Added date formatter utility --- src/main/java/utils/DateUtilities.java | 47 ++++++++++++-------------- src/test/java/AppTest.java | 14 ++++++++ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/main/java/utils/DateUtilities.java b/src/main/java/utils/DateUtilities.java index 245a1d4..14d8c6a 100644 --- a/src/main/java/utils/DateUtilities.java +++ b/src/main/java/utils/DateUtilities.java @@ -223,32 +223,6 @@ public static String getCurrentDate(ZoneIds zoneId) { return dtf.format(now); } - /** - * Formats a date string from an automatically detected input format to a - * user-specified output format. - * - * @param input The date string to format. - * @param outputFormat The desired output format string (e.g., "yyyy-MM-dd"). - * @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) { - 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" - }; - for (String inputFormat : SUPPORTED_INPUT_FORMATS) { - try { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(inputFormat); - Date date = simpleDateFormat.parse(input); - SimpleDateFormat outputSimpleDateFormat = new SimpleDateFormat(outputFormat); - return outputSimpleDateFormat.format(date); - } - catch (ParseException ignored) {} - } - return input; - } - /** * Formats a date string from a specified input format to a desired output format. * @@ -268,4 +242,25 @@ public static String fixDateFormat(String input, String inputFormat, String outp } catch (ParseException exception) {throw new RuntimeException(exception);} } + + /** + * Formats a date string from an automatically detected input format to a + * user-specified output format. + * + * @param input The date string to format. + * @param outputFormat The desired output format string (e.g., "yyyy-MM-dd"). + * @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) { + 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" + }; + for (String inputFormat : SUPPORTED_INPUT_FORMATS) { + try {return fixDateFormat(input, inputFormat, outputFormat);} + catch (Exception ignored) {} + } + return input; + } } diff --git a/src/test/java/AppTest.java b/src/test/java/AppTest.java index eff9e57..81dcaca 100644 --- a/src/test/java/AppTest.java +++ b/src/test/java/AppTest.java @@ -336,5 +336,19 @@ public void jsonSchemaTest() { "{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"category\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}}},\"name\":{\"type\":\"string\"},\"photoUrls\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"tags\":{\"type\":\"array\",\"items\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}}}},\"status\":{\"type\":\"string\"}}}", petSchema.toString() ); + printer.success("The jsonSchemaTest() test pass!"); + } + + @Test + public void dateFormatTest() { + String date = "2025-6-20"; + String expectedDate = "2025-06-20"; + + Assert.assertEquals( + "Fixed date format did not match the expected one!", + expectedDate, + DateUtilities.fixDateFormat(date, "yyyy-MM-dd") + ); + printer.success("The dateFormatTest() test pass!"); } }