Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/utils/DateUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions src/test/java/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}