Skip to content

Commit 4d0e7fd

Browse files
committed
Added date formatter utility
1 parent b972ccd commit 4d0e7fd

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

src/main/java/utils/DateUtilities.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -223,32 +223,6 @@ public static String getCurrentDate(ZoneIds zoneId) {
223223
return dtf.format(now);
224224
}
225225

226-
/**
227-
* Formats a date string from an automatically detected input format to a
228-
* user-specified output format.
229-
*
230-
* @param input The date string to format.
231-
* @param outputFormat The desired output format string (e.g., "yyyy-MM-dd").
232-
* @return The formatted date string, or the original input string if the
233-
* input format cannot be detected.
234-
*/
235-
public static String fixDateFormat(String input, String outputFormat) {
236-
String[] SUPPORTED_INPUT_FORMATS = {
237-
"yyyy-M-dd", "yyyy-MM-dd", "M/d/yyyy", "MM/d/yyyy", "yyyy/M/d", "yyyy/MM/d",
238-
"M-d-yyyy", "MM-d-yyyy", "yyyy-M-d", "yyyy-MM-d"
239-
};
240-
for (String inputFormat : SUPPORTED_INPUT_FORMATS) {
241-
try {
242-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(inputFormat);
243-
Date date = simpleDateFormat.parse(input);
244-
SimpleDateFormat outputSimpleDateFormat = new SimpleDateFormat(outputFormat);
245-
return outputSimpleDateFormat.format(date);
246-
}
247-
catch (ParseException ignored) {}
248-
}
249-
return input;
250-
}
251-
252226
/**
253227
* Formats a date string from a specified input format to a desired output format.
254228
*
@@ -268,4 +242,25 @@ public static String fixDateFormat(String input, String inputFormat, String outp
268242
}
269243
catch (ParseException exception) {throw new RuntimeException(exception);}
270244
}
245+
246+
/**
247+
* Formats a date string from an automatically detected input format to a
248+
* user-specified output format.
249+
*
250+
* @param input The date string to format.
251+
* @param outputFormat The desired output format string (e.g., "yyyy-MM-dd").
252+
* @return The formatted date string, or the original input string if the
253+
* input format cannot be detected.
254+
*/
255+
public static String fixDateFormat(String input, String outputFormat) {
256+
String[] SUPPORTED_INPUT_FORMATS = {
257+
"yyyy-M-dd", "yyyy-MM-dd", "M/d/yyyy", "MM/d/yyyy", "yyyy/M/d", "yyyy/MM/d",
258+
"M-d-yyyy", "MM-d-yyyy", "yyyy-M-d", "yyyy-MM-d"
259+
};
260+
for (String inputFormat : SUPPORTED_INPUT_FORMATS) {
261+
try {return fixDateFormat(input, inputFormat, outputFormat);}
262+
catch (Exception ignored) {}
263+
}
264+
return input;
265+
}
271266
}

src/test/java/AppTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,5 +336,19 @@ public void jsonSchemaTest() {
336336
"{\"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\"}}}",
337337
petSchema.toString()
338338
);
339+
printer.success("The jsonSchemaTest() test pass!");
340+
}
341+
342+
@Test
343+
public void dateFormatTest() {
344+
String date = "2025-6-20";
345+
String expectedDate = "2025-06-20";
346+
347+
Assert.assertEquals(
348+
"Fixed date format did not match the expected one!",
349+
expectedDate,
350+
DateUtilities.fixDateFormat(date, "yyyy-MM-dd")
351+
);
352+
printer.success("The dateFormatTest() test pass!");
339353
}
340354
}

0 commit comments

Comments
 (0)