사용자 도구

사이트 도구


java:8:datetime

Java 8 Date & Time

java.util.Date <-> LocalDate/LocalDateTime/Instant간 변환

/**
 * Calls {@link #asLocalDate(Date, ZoneId)} with the system default time zone.
 */
public static LocalDate asLocalDate(java.util.Date date) {
    return asLocalDate(date, ZoneId.systemDefault());
}
 
/**
 * Creates {@link LocalDate} from {@code java.util.Date} or it's subclasses. Null-safe.
 */
public static LocalDate asLocalDate(java.util.Date date, ZoneId zone) {
    if (date == null) {
        return null;
    }
 
    if (date instanceof java.sql.Date) {
        return ((java.sql.Date) date).toLocalDate();
    } else {
        return Instant.ofEpochMilli(date.getTime()).atZone(zone).toLocalDate();
    }
}
 
/**
 * Calls {@link #asLocalDateTime(Date, ZoneId)} with the system default time zone.
 */
public static LocalDateTime asLocalDateTime(java.util.Date date) {
    return asLocalDateTime(date, ZoneId.systemDefault());
}
 
/**
 * Creates {@link LocalDateTime} from {@code java.util.Date} or it's subclasses. Null-safe.
 */
public static LocalDateTime asLocalDateTime(java.util.Date date, ZoneId zone) {
    if (date == null) {
        return null;
    }
 
    if (date instanceof java.sql.Timestamp) {
        return ((java.sql.Timestamp) date).toLocalDateTime();
    } else {
        return Instant.ofEpochMilli(date.getTime()).atZone(zone).toLocalDateTime();
    }
}
 
/**
 * Calls {@link #asUtilDate(Object, ZoneId)} with the system default time zone.
 */
public static java.util.Date asUtilDate(Object date) {
    return asUtilDate(date, ZoneId.systemDefault());
}
 
/**
 * Creates a {@link java.util.Date} from various date objects. Is null-safe. Currently supports:<ul>
 * <li>{@link java.util.Date}
 * <li>{@link java.sql.Date}
 * <li>{@link java.sql.Timestamp}
 * <li>{@link java.time.LocalDate}
 * <li>{@link java.time.LocalDateTime}
 * <li>{@link java.time.ZonedDateTime}
 * <li>{@link java.time.Instant}
 * </ul>
 *
 * @param zone Time zone, used only if the input object is LocalDate or LocalDateTime.
 *
 * @return {@link java.util.Date} (exactly this class, not a subclass, such as java.sql.Date)
 */
public static java.util.Date asUtilDate(Object date, ZoneId zone) {
    if (date == null) {
        return null;
    }
 
    if (date instanceof java.sql.Date || date instanceof java.sql.Timestamp) {
        return new java.util.Date(((java.util.Date) date).getTime());
    }
    if (date instanceof java.util.Date) {
        return (java.util.Date) date;
    }
    if (date instanceof LocalDate) {
        return Date.from(((LocalDate) date).atStartOfDay(zone).toInstant());
    }
    if (date instanceof LocalDateTime) {
        return Date.from(((LocalDateTime) date).atZone(zone).toInstant());
    }
    if (date instanceof ZonedDateTime) {
        return Date.from(((ZonedDateTime) date).toInstant());
    }
    if (date instanceof Instant) {
        return Date.from((Instant) date);
    }
 
    throw new UnsupportedOperationException("Don't know hot to convert " + date.getClass().getName() + " to java.util.Date");
}
 
/**
 * Creates an {@link Instant} from {@code java.util.Date} or it's subclasses. Null-safe.
 */
public static Instant asInstant(Date date) {
    if (date == null) {
        return null;
    } else {
        return Instant.ofEpochMilli(date.getTime());
    }
}
 
/**
 * Calls {@link #asZonedDateTime(Date, ZoneId)} with the system default time zone.
 */
public static ZonedDateTime asZonedDateTime(Date date) {
    return asZonedDateTime(date, ZoneId.systemDefault());
}
 
/**
 * Creates {@link ZonedDateTime} from {@code java.util.Date} or it's subclasses. Null-safe.
 */
public static ZonedDateTime asZonedDateTime(Date date, ZoneId zone) {
    if (date == null) {
        return null;
    } else {
        return asInstant(date).atZone(zone);
    }
}

Time Mocking for test

Duration 기반으로 흐른 시간 검사

var start = Instant.now();
Thread.sleep(2042);
var duration = Duration.between(start,Instant.now());
System.out.printf("%d seconds and %d ms",duration.toSeconds(),duration.toMillisPart());

systemDefaultTimeZone 과 LocalDateTime

  • java.time.ClocksystemDefaultTimeZone()System.currentTimeMilliss()를 사용하기 때문에 nanosecond 부분은 모두 잘린다.
  • 그리고 java.time.LocalDateTimenow() 메소드는 systemDefaultTimeZone() clock을 사용하기 때문에 결과적으로 nanosecond 부분이 잘린다.
  • 따라서, LocalDateTime.now()를 여러차례 호출할때 nanosecond 의 시간차이만 날 뿐이라면 동일한 값이 반환될 수 있다.
  • 이로인해 Test 코드에서 LocalDateTime.now()를 두번 연속 호출하고서 두 값이 서로 다를것이라는 예상으로 (firstNow.isBefore(secondNow) == true) 작성한 코드는 상황에 따라 틀려지게 된다.
java/8/datetime.txt · 마지막으로 수정됨: 2022/05/11 16:47 저자 kwon37xi