|
9 | 9 | import java.time.temporal.ChronoUnit; |
10 | 10 | import java.util.Calendar; |
11 | 11 | import java.util.Date; |
| 12 | +import java.util.regex.Matcher; |
| 13 | +import java.util.regex.Pattern; |
12 | 14 |
|
13 | 15 | public class DateUtilities { |
14 | 16 |
|
@@ -220,4 +222,56 @@ public static String getCurrentDate(ZoneIds zoneId) { |
220 | 222 | DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
221 | 223 | return dtf.format(now); |
222 | 224 | } |
| 225 | + |
| 226 | + /** |
| 227 | + * Fixes a date string assuming the input format is "yyyy-M-dd". |
| 228 | + * |
| 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. |
| 232 | + */ |
| 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}"); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Fixes a date string based on a provided input pattern and formats it according to an output pattern. |
| 243 | + * |
| 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. |
| 249 | + */ |
| 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; |
| 275 | + } |
| 276 | + } |
223 | 277 | } |
0 commit comments