사용자 도구

사이트 도구


java:8:datetime

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
다음 판 양쪽 다음 판
java:8:datetime [2016/07/04 14:46]
kwon37xi
java:8:datetime [2018/07/25 14:27]
kwon37xi
줄 1: 줄 1:
 ====== Java 8 Date & Time ====== ====== Java 8 Date & Time ======
   * [[http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html|Java SE 8 Date and Time]]   * [[http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html|Java SE 8 Date and Time]]
 +  * [[http://www.mkyong.com/java8/java-8-convert-date-to-localdate-and-localdatetime/|Java 8 – Convert Date to LocalDate and LocalDateTime]]
 +  * [[https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME|DateTimeFormatter#ISO_LOCAL_DATE_TIME]] : LocalDateTime ISO 8601 표준 포맷터 ''2011-12-03T10:15:30''
 +  * [[https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME|DateTimeFormatter#ISO_OFFSET_DATE_TIME]] : Offset DateTime ISO 8601 표준 포맷터 ''2011-12-03T10:15:30+01:00''
 +===== java.util.Date <-> LocalDate/LocalDateTime/Instant간 변환 =====
 +  * [[http://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate|datetime - Convert java.util.Date to java.time.LocalDate]]
 +
 +<code java>
 +/**
 + * 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);
 +    }
 +}
 +</code>
 +
  
 ===== Time Mocking for test ===== ===== Time Mocking for test =====
   * refer to [[http://docs.oracle.com/javase/8/docs/api/java/time/Clock.html|java.time.Clock]]   * refer to [[http://docs.oracle.com/javase/8/docs/api/java/time/Clock.html|java.time.Clock]]
   * [[http://blog.freeside.co/2015/01/15/fixing-current-time-for-tests-with-java-8-s-date-time-api/|Ad-Hockery - Fixing current time for tests with Java 8's date/time API]]   * [[http://blog.freeside.co/2015/01/15/fixing-current-time-for-tests-with-java-8-s-date-time-api/|Ad-Hockery - Fixing current time for tests with Java 8's date/time API]]
 +  * [[http://www.baeldung.com/java-override-system-time|Overriding System Time for Testing in Java | Baeldung]]
 +
java/8/datetime.txt · 마지막으로 수정됨: 2022/05/11 16:47 저자 kwon37xi