diff --git a/src/main/java/utils/DateUtilities.java b/src/main/java/utils/DateUtilities.java index 70f6b95..14d8c6a 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,45 @@ public static String getCurrentDate(ZoneIds zoneId) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 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. + * + * @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!"); } }