diff --git a/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/util/DateUtil.java b/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/util/DateUtil.java new file mode 100644 index 0000000..5cd8009 --- /dev/null +++ b/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/util/DateUtil.java @@ -0,0 +1,85 @@ +package com.klaro.acquirecore.framework.util; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.time.temporal.ChronoUnit; + +/** + * Static utility class for date operations migrated from legacy C code. + * Provides parsing, formatting, and calculation for LocalDate instances. + */ +public final class DateUtil { + + private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; + private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT); + + private DateUtil() { + // Utility class - prevent instantiation + } + + public static LocalDate parse(String dateStr) { + if (dateStr == null || dateStr.isBlank()) { + return null; + } + try { + return LocalDate.parse(dateStr.trim(), DEFAULT_FORMATTER); + } catch (DateTimeParseException e) { + return null; + } + } + + public static String format(LocalDate date) { + if (date == null) { + return ""; + } + return date.format(DEFAULT_FORMATTER); + } + + public static String format(LocalDate date, String pattern) { + if (date == null || pattern == null || pattern.isBlank()) { + return ""; + } + try { + return date.format(DateTimeFormatter.ofPattern(pattern)); + } catch (IllegalArgumentException e) { + return ""; + } + } + + public static long diffDays(LocalDate from, LocalDate to) { + if (from == null || to == null) { + return 0; + } + return ChronoUnit.DAYS.between(from, to); + } + + public static LocalDate addDays(LocalDate date, long days) { + if (date == null) { + return null; + } + return date.plusDays(days); + } + + public static LocalDate today() { + return LocalDate.now(); + } + + public static boolean isValid(String dateStr) { + return parse(dateStr) != null; + } + + public static boolean isBefore(LocalDate date, LocalDate before) { + if (date == null || before == null) { + return false; + } + return date.isBefore(before); + } + + public static boolean isAfter(LocalDate date, LocalDate after) { + if (date == null || after == null) { + return false; + } + return date.isAfter(after); + } +}