273 lines
6.6 KiB
Java
273 lines
6.6 KiB
Java
package com.kri.common.feature.datetime;
|
|
|
|
import java.time.Instant;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.time.OffsetDateTime;
|
|
import java.time.ZoneId;
|
|
import java.time.ZoneOffset;
|
|
import java.time.ZonedDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeParseException;
|
|
import java.util.Objects;
|
|
|
|
public final class DateTimeUtil {
|
|
|
|
public static final ZoneId KST = ZoneId.of("Asia/Seoul");
|
|
public static final ZoneId UTC = ZoneOffset.UTC;
|
|
/**
|
|
* 날짜+시간: 2025-11-28 15:00:14
|
|
*/
|
|
public static final DateTimeFormatter DATETIME_FMT =
|
|
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
/**
|
|
* 날짜: 2025-11-28
|
|
*/
|
|
public static final DateTimeFormatter DATE_FMT =
|
|
DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
private DateTimeUtil() {
|
|
}
|
|
|
|
/* =======================
|
|
* Now (KST)
|
|
* ======================= */
|
|
|
|
/**
|
|
* 현재 KST 날짜+시간 문자열
|
|
*/
|
|
public static String nowKstDateTime() {
|
|
return formatKst(Instant.now(), DATETIME_FMT);
|
|
}
|
|
|
|
/**
|
|
* 현재 KST 날짜 문자열
|
|
*/
|
|
public static String nowKstDate() {
|
|
return nowKstZoned().format(DATE_FMT);
|
|
}
|
|
|
|
/**
|
|
* 현재 KST ZonedDateTime
|
|
*/
|
|
public static ZonedDateTime nowKstZoned() {
|
|
return Instant.now().atZone(KST);
|
|
}
|
|
|
|
/**
|
|
* 현재 KST LocalDateTime
|
|
*/
|
|
public static LocalDateTime nowKstLocalDateTime() {
|
|
return nowKstZoned().toLocalDateTime();
|
|
}
|
|
|
|
/**
|
|
* 현재 KST LocalDate
|
|
*/
|
|
public static LocalDate nowKstLocalDate() {
|
|
return nowKstZoned().toLocalDate();
|
|
}
|
|
|
|
/* =======================
|
|
* Format (Instant -> String)
|
|
* ======================= */
|
|
|
|
/**
|
|
* Instant -> KST 날짜+시간 문자열
|
|
*/
|
|
public static String formatKstDateTime(Instant instant) {
|
|
return formatKst(instant, DATETIME_FMT);
|
|
}
|
|
|
|
/**
|
|
* Instant -> KST 날짜 문자열
|
|
*/
|
|
public static String formatKstDate(Instant instant) {
|
|
return formatKst(instant, DATE_FMT);
|
|
}
|
|
|
|
/**
|
|
* Instant -> KST 문자열 (커스텀 포맷)
|
|
*/
|
|
public static String formatKst(Instant instant, DateTimeFormatter formatter) {
|
|
if (instant == null) {
|
|
return null;
|
|
}
|
|
final DateTimeFormatter fmt = (formatter != null) ? formatter : DATETIME_FMT;
|
|
return instant.atZone(KST).format(fmt);
|
|
}
|
|
|
|
/* =======================
|
|
* Validate
|
|
* ======================= */
|
|
|
|
/**
|
|
* "yyyy-MM-dd" 형태인지
|
|
*/
|
|
public static boolean isDate(String text) {
|
|
return tryParseLocalDate(text, DATE_FMT) != null;
|
|
}
|
|
|
|
/**
|
|
* "yyyy-MM-dd HH:mm:ss" 형태인지
|
|
*/
|
|
public static boolean isDateTime(String text) {
|
|
return tryParseLocalDateTime(text, DATETIME_FMT) != null;
|
|
}
|
|
|
|
/**
|
|
* ISO Instant / ISO OffsetDateTime / ISO ZonedDateTime 중 하나인지
|
|
*/
|
|
public static boolean isIsoDateTime(String text) {
|
|
if (text == null || text.isBlank()) {
|
|
return false;
|
|
}
|
|
|
|
final String s = text.trim();
|
|
try {
|
|
Instant.parse(s);
|
|
return true;
|
|
} catch (DateTimeParseException ignored) {
|
|
}
|
|
try {
|
|
OffsetDateTime.parse(s);
|
|
return true;
|
|
} catch (DateTimeParseException ignored) {
|
|
}
|
|
try {
|
|
ZonedDateTime.parse(s);
|
|
return true;
|
|
} catch (DateTimeParseException ignored) {
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/* =======================
|
|
* Parse (String -> Instant)
|
|
* ======================= */
|
|
|
|
/**
|
|
* ISO 계열 문자열을 Instant로 파싱 (Instant/OffsetDateTime/ZonedDateTime 순으로 시도)
|
|
*/
|
|
public static Instant parseToInstant(String text) {
|
|
if (text == null || text.isBlank()) {
|
|
return null;
|
|
}
|
|
|
|
final String s = text.trim();
|
|
try {
|
|
return Instant.parse(s);
|
|
} catch (DateTimeParseException ignored) {
|
|
}
|
|
try {
|
|
return OffsetDateTime.parse(s).toInstant();
|
|
} catch (DateTimeParseException ignored) {
|
|
}
|
|
try {
|
|
return ZonedDateTime.parse(s).toInstant();
|
|
} catch (DateTimeParseException ignored) {
|
|
}
|
|
|
|
throw new DateTimeParseException("Unsupported datetime format", s, 0);
|
|
}
|
|
|
|
/**
|
|
* 커스텀 포맷 문자열을 KST 기준으로 해석해서 Instant로 변환 예) "2025-11-28 15:00:14", formatter=DATETIME_FMT
|
|
*/
|
|
public static Instant parseKstToInstant(String text, DateTimeFormatter formatter) {
|
|
if (text == null || text.isBlank()) {
|
|
return null;
|
|
}
|
|
Objects.requireNonNull(formatter, "formatter");
|
|
|
|
LocalDateTime ldt = LocalDateTime.parse(text.trim(), formatter);
|
|
return ldt.atZone(KST).toInstant();
|
|
}
|
|
|
|
/**
|
|
* "yyyy-MM-dd HH:mm:ss" 포맷을 KST로 해석
|
|
*/
|
|
public static Instant parseKstDateTimeToInstant(String text) {
|
|
return parseKstToInstant(text, DATETIME_FMT);
|
|
}
|
|
|
|
/**
|
|
* "yyyy-MM-dd" 포맷을 KST 자정(00:00:00)으로 해석
|
|
*/
|
|
public static Instant parseKstDateToInstant(String text) {
|
|
if (text == null || text.isBlank()) {
|
|
return null;
|
|
}
|
|
LocalDate ld = LocalDate.parse(text.trim(), DATE_FMT);
|
|
return ld.atStartOfDay(KST).toInstant();
|
|
}
|
|
|
|
/* =======================
|
|
* Convert / Helpers
|
|
* ======================= */
|
|
|
|
/**
|
|
* Instant -> KST ZonedDateTime
|
|
*/
|
|
public static ZonedDateTime toKstZoned(Instant instant) {
|
|
return instant == null ? null : instant.atZone(KST);
|
|
}
|
|
|
|
/**
|
|
* Instant -> KST LocalDateTime
|
|
*/
|
|
public static LocalDateTime toKstLocalDateTime(Instant instant) {
|
|
ZonedDateTime zdt = toKstZoned(instant);
|
|
return zdt == null ? null : zdt.toLocalDateTime();
|
|
}
|
|
|
|
/**
|
|
* Instant -> KST LocalDate
|
|
*/
|
|
public static LocalDate toKstLocalDate(Instant instant) {
|
|
ZonedDateTime zdt = toKstZoned(instant);
|
|
return zdt == null ? null : zdt.toLocalDate();
|
|
}
|
|
|
|
/**
|
|
* null/blank 안전 trim
|
|
*/
|
|
public static String trimToNull(String s) {
|
|
if (s == null) {
|
|
return null;
|
|
}
|
|
String t = s.trim();
|
|
return t.isEmpty() ? null : t;
|
|
}
|
|
|
|
public static LocalDate parseLocalDate(String text) {
|
|
return tryParseLocalDate(text, DATE_FMT);
|
|
}
|
|
|
|
public static LocalDateTime parseLocalDateTime(String text) {
|
|
return tryParseLocalDateTime(text, DATETIME_FMT);
|
|
}
|
|
|
|
private static LocalDate tryParseLocalDate(String text, DateTimeFormatter fmt) {
|
|
if (text == null || text.isBlank()) {
|
|
return null;
|
|
}
|
|
try {
|
|
return LocalDate.parse(text.trim(), fmt);
|
|
} catch (DateTimeParseException e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static LocalDateTime tryParseLocalDateTime(String text, DateTimeFormatter fmt) {
|
|
if (text == null || text.isBlank()) {
|
|
return null;
|
|
}
|
|
try {
|
|
return LocalDateTime.parse(text.trim(), fmt);
|
|
} catch (DateTimeParseException e) {
|
|
return null;
|
|
}
|
|
}
|
|
} |