Skip to content

Commit a85451d

Browse files
authored
Merge pull request #127 from Umutayb/date-formatter
Fixed versioning and removed redundant method
2 parents 1404eb3 + e2a22fa commit a85451d

File tree

3 files changed

+83
-27
lines changed

3 files changed

+83
-27
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.umutayb</groupId>
88
<artifactId>Utilities</artifactId>
9-
<version>1.7.4</version>
9+
<version>1.7.5</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java-Utilities</name>

src/main/java/utils/DateUtilities.java

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

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-
246226
/**
247227
* Formats a date string from an automatically detected input format to a
248228
* user-specified output format.
@@ -252,14 +232,90 @@ public static String fixDateFormat(String input, String inputFormat, String outp
252232
* @return The formatted date string, or the original input string if the
253233
* input format cannot be detected.
254234
*/
255-
public static String fixDateFormat(String input, String outputFormat) {
235+
public static String reformatDateString(String input, String outputFormat) {
256236
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"
237+
// Standard formats
238+
"yyyy-M-dd",
239+
"yyyy-MM-dd",
240+
"M/d/yyyy",
241+
"MM/d/yyyy",
242+
"yyyy/M/d",
243+
"yyyy/MM/d",
244+
"M-d-yyyy",
245+
"MM-d-yyyy",
246+
"yyyy-M-d",
247+
"yyyy-MM-d",
248+
249+
// Variations with different separators
250+
"yyyy.M.dd",
251+
"yyyy.MM.dd",
252+
"M.d.yyyy",
253+
"MM.d.yyyy",
254+
"yyyy.M.d",
255+
"yyyy.MM.d",
256+
"M-d-yyyy",
257+
"MM-d-yyyy",
258+
259+
// Single digit month/day
260+
"y-M-d",
261+
"y-MM-d",
262+
"M/d/yy",
263+
"MM/d/yy",
264+
"y/M/d",
265+
"y/MM/d",
266+
"M-d-yy",
267+
"MM-d-yy",
268+
269+
//No separators
270+
"yyyyMdd",
271+
"yyMdd",
272+
"yyyyMd",
273+
"yyMd",
274+
"Myyyy",
275+
"Mddyy",
276+
"Mdyyyy",
277+
278+
//Common International Formats
279+
"dd/M/yyyy",
280+
"dd/MM/yyyy",
281+
"dd-M-yyyy",
282+
"dd-MM-yyyy",
283+
"d/M/yyyy",
284+
"d/MM/yyyy",
285+
286+
//More variations
287+
"yyyyMdd",
288+
"yyMdd",
289+
"yyyyMd",
290+
"yyMd",
291+
"Mdyyyy",
292+
"Mddyy",
293+
"Myyyy",
294+
295+
// Short year format
296+
"M/d/yy",
297+
"MM/d/yy",
298+
"M-d-yy",
299+
"MM-d-yy",
300+
"dd/M/yy",
301+
"dd/MM/yy",
302+
303+
//More separators
304+
"M/d/yyyy",
305+
"MM/d/yyyy",
306+
"M-d-yyyy",
307+
"MM-d-yyyy",
308+
"d/M/yyyy",
309+
"d/MM/yyyy",
310+
311+
//No Separators
312+
"yyyyMMdd",
313+
"yyMMdd",
259314
};
260315
for (String inputFormat : SUPPORTED_INPUT_FORMATS) {
261-
try {return fixDateFormat(input, inputFormat, outputFormat);}
262-
catch (Exception ignored) {}
316+
String formattedDate = reformatDateString(input, inputFormat, outputFormat);
317+
if (formattedDate == null) continue;
318+
return formattedDate;
263319
}
264320
return input;
265321
}

src/test/java/AppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public void dateFormatTest() {
347347
Assert.assertEquals(
348348
"Fixed date format did not match the expected one!",
349349
expectedDate,
350-
DateUtilities.fixDateFormat(date, "yyyy-MM-dd")
350+
DateUtilities.reformatDateString(date, "yyyy-MM-dd")
351351
);
352352
printer.success("The dateFormatTest() test pass!");
353353
}

0 commit comments

Comments
 (0)