Skip to content

Commit b972ccd

Browse files
committed
Update DateUtilities.java
1 parent 83ff17a commit b972ccd

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

src/main/java/utils/DateUtilities.java

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -224,54 +224,48 @@ public static String getCurrentDate(ZoneIds zoneId) {
224224
}
225225

226226
/**
227-
* Fixes a date string assuming the input format is "yyyy-M-dd".
227+
* Formats a date string from an automatically detected input format to a
228+
* user-specified output format.
228229
*
229-
* @param input The date string to fix, expected in "yyyy-M-dd" format.
230-
* @return The formatted date string in "yyyy-MM-dd" format, or the original input if it doesn't match.
231-
* @throws IllegalArgumentException if input is null.
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.
232234
*/
233-
public static String fixDateFormat(String input) {
234-
// Regex to match the pattern "yyyy-M-dd" where M is a single digit month
235-
Pattern pattern = Pattern.compile("(\\d{4})-(\\d)-(\\d{2})");
236-
Matcher matcher = pattern.matcher(input);
237-
238-
return fixDateFormat(input, "(\\d{4})-(\\d)-(\\d{2})", "{1}-{2}-{3}");
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;
239250
}
240251

241252
/**
242-
* Fixes a date string based on a provided input pattern and formats it according to an output pattern.
253+
* Formats a date string from a specified input format to a desired output format.
243254
*
244-
* @param input The date string to fix and format.
245-
* @param inputPattern The regular expression pattern to match the input date format. Capture groups should be used to extract date components.
246-
* @param outputPattern The desired output format string. Use `{1}`, `{2}`, etc. to refer to captured groups from the input pattern.
247-
* @return The formatted date string if the input matches the pattern, otherwise the original input string.
248-
* @throws IllegalArgumentException if input, inputPattern, or outputPattern is null.
255+
* @param input The date string to format.
256+
* @param inputFormat The format of the input date string (e.g., "yyyy-MM-dd").
257+
* @param outputFormat The desired format of the output date string (e.g., "MM/dd/yyyy").
258+
* @return The formatted date string.
259+
* @throws RuntimeException If the input date string cannot be parsed according to the specified input format.
260+
* The exception is a `RuntimeException` wrapping the original `ParseException`.
249261
*/
250-
public static String fixDateFormat(String input, String inputPattern, String outputPattern) {
251-
// Build the regex pattern dynamically
252-
Pattern pattern = Pattern.compile(inputPattern);
253-
Matcher matcher = pattern.matcher(input);
254-
255-
if (matcher.matches()) {
256-
// Extract groups based on the number of capture groups in the input pattern
257-
String[] groups = new String[matcher.groupCount()];
258-
for (int i = 1; i <= matcher.groupCount(); i++) {
259-
groups[i - 1] = matcher.group(i);
260-
}
261-
262-
// Format the output based on the output pattern. This is a simplified example
263-
// and might need adjustments for more complex output formats. It assumes
264-
// the output pattern uses numbered placeholders like {1}, {2}, etc.
265-
String formattedDate = outputPattern;
266-
for (int i = 0; i < groups.length; i++) {
267-
formattedDate = formattedDate.replace("{" + (i + 1) + "}", groups[i]);
268-
}
269-
270-
return formattedDate;
271-
272-
} else {
273-
// If the input doesn't match the expected format, return it unchanged.
274-
return input;
262+
public static String fixDateFormat(String input, String inputFormat, String outputFormat) {
263+
try {
264+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(inputFormat);
265+
Date date = simpleDateFormat.parse(input);
266+
SimpleDateFormat outputSimpleDateFormat = new SimpleDateFormat(outputFormat);
267+
return outputSimpleDateFormat.format(date);
275268
}
269+
catch (ParseException exception) {throw new RuntimeException(exception);}
276270
}
277271
}

0 commit comments

Comments
 (0)