Skip to content

Commit 1404eb3

Browse files
authored
Merge pull request #126 from Umutayb/date-formatter
Date formatter
2 parents 48871ea + 4d0e7fd commit 1404eb3

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/main/java/utils/DateUtilities.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.time.temporal.ChronoUnit;
1010
import java.util.Calendar;
1111
import java.util.Date;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
1214

1315
public class DateUtilities {
1416

@@ -220,4 +222,45 @@ public static String getCurrentDate(ZoneIds zoneId) {
220222
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
221223
return dtf.format(now);
222224
}
225+
226+
/**
227+
* Formats a date string from a specified input format to a desired output format.
228+
*
229+
* @param input The date string to format.
230+
* @param inputFormat The format of the input date string (e.g., "yyyy-MM-dd").
231+
* @param outputFormat The desired format of the output date string (e.g., "MM/dd/yyyy").
232+
* @return The formatted date string.
233+
* @throws RuntimeException If the input date string cannot be parsed according to the specified input format.
234+
* The exception is a `RuntimeException` wrapping the original `ParseException`.
235+
*/
236+
public static String fixDateFormat(String input, String inputFormat, String outputFormat) {
237+
try {
238+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(inputFormat);
239+
Date date = simpleDateFormat.parse(input);
240+
SimpleDateFormat outputSimpleDateFormat = new SimpleDateFormat(outputFormat);
241+
return outputSimpleDateFormat.format(date);
242+
}
243+
catch (ParseException exception) {throw new RuntimeException(exception);}
244+
}
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+
}
223266
}

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)