ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Date와 Time
    JAVA/JAVA8 관련 2022. 5. 15. 17:12

    기계적인 시간과 인류용 시간 표현방법

     

    1. 기계적인 시간

    • Instant.now(): 현재 UTC (GMT)를 리턴한다.
    • Universal Time Coordinated == Greenwich Mean Time
    public class MachineTime {
    
        public static void main(String[] args) {
            Instant instant = Instant.now();//기준 시 UTC (GMT)
            System.out.println("Instant :: " + instant);
            ZoneId zone = ZoneId.systemDefault();
            System.out.println("Zone :: " + zone);
            ZonedDateTime zonedDateTime = instant.atZone(zone);
            System.out.println("ZonedDateTime :: " + zonedDateTime);
        }
    }

    - ZoneId.systemDefault()

    : 현재 시스템상의 zone 정보를 반환함. (예시 : Asia/Seoul)

    - instant.atZone(zone)

    : UTC 기준이 아닌 zone의 위치 기반의 시간을 반환합니다.

     

    2. 인류용 시간

    : LocalDate와 LocalDateTime이 존재

    LocalDate : 년/월/일 까지 표현

    LocalDateTime : 년/월/일/시/분/초 까지 표현

    public class HumanTime {
    
        public static void main(String[] args) {
            LocalDateTime now = LocalDateTime.now();
            System.out.println("now :: " + now);
            LocalDateTime birthDay = LocalDateTime.of(1999, Month.JULY, 10, 0, 0, 0);
    
            ZonedDateTime nowZoneId = ZonedDateTime.now(ZoneId.of("UTC"));
            System.out.println("nowZoneId :: " + nowZoneId);
    
            ZonedDateTime zonedDateTime = ZonedDateTime.of(1999, Month.JULY.getValue(),10,0,0,0,0, ZoneId.of("UTC"));
            System.out.println("zonedDateTime :: " + zonedDateTime);
        }
    }

    - LocalDateTime.now()

    : 현재 시스템 zone에 해당하는 로컬 일시를 반환

    - LocalDateTime.of(1988.Month.1999, 11, 10, 0, 0);

    : 특정 일시를 반환

    - ZonedDateTime.now(ZoneId.of("UTC"));

    : 특정 Zone의 현재 일시를 반환

     

    3. 기간을 표현하는 방법

    public class PeroidDuration {
    
        public static void main(String[] args) {
    
            LocalDate today = LocalDate.now();
            LocalDate birthDay = LocalDate.of(2020, Month.JULY, 10);
    
            Period period = Period.between(birthDay, today);
            System.out.println(period.getMonths()+"개월 "+period.getDays()+"일");
    
            Period until = today.until(birthDay);
            System.out.println("Period.until.get(ChronoUnit.Month) = " + until.get(ChronoUnit.MONTHS));
            System.out.println("Period.until.get(ChronoUnit.DAYS) = " + until.get(ChronoUnit.DAYS));
    
            Instant now = Instant.now();
            Instant plus = now.plus(10, ChronoUnit.SECONDS);
            Duration duration = Duration.between(now, plus);
            System.out.println(duration.getSeconds());
        }
    }

    - Period : 날짜 기간 단위를 표현할 때 사용

    - Duration : 시간 단위 기간을 표현할 때 사용

     

    - Peroid.between(date1, date2)

    : date1과 date2를 비교하여 Period 타입의 인스턴스로 반환

    - Period until = today.unitl(date1)

    :  LocalDate 객체의 Until 메서드를 통하여 전달한 인자값과 기간을 계산한

    Period 타입의 인스턴스를 반환

    - Duration.between(now, plus)

    : 두 인스턴스간의 시간을 비교하여 Duration 타입의 인스턴스로 반환

     

    4. 기간을 표현하는 방법

     

    public class DateParser {
        public static void main(String[] args) {
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter MMddyyyy = DateTimeFormatter.ofPattern("MM/dd/yyyy");
            System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));
            System.out.println(now.format(MMddyyyy));
            LocalDate date = LocalDate.parse("05/23/1999", MMddyyyy);
            System.out.println("date :: " + date);
        }
    }

    - DateTimeFormatter.ofPattern("MM/dd/yyyy")

    : DateTimeFormatter를 이용해 각종 형식으로 LocalDateTime을 포맷팅할 수 있음.

    - now.format(MMddyyyy)

    : ofPattern으로 지정한 패턴으로 DateTimeFormat을 세팅함.

    - LocalDate date = LocalDate.parse("05/23/1999", MMddyyyy);

    : 문자열을 oofPattern에서 선언한 패턴 방식으로 파싱하여 LocalDate 타입의 인스턴스를 반환

     

    레거시 API 지원

    • GregorianCalendar와 Date 타입의 인스턴스를 Instant나 ZonedDateTime으로 변환 가능.
    • java.util.TimeZone에서 java.time.ZoneId로 상호 변환 가능.

     

    'JAVA > JAVA8 관련' 카테고리의 다른 글

    Concurrent - Executor  (0) 2022.05.22
    Concurrent 프로그래밍 소개  (0) 2022.05.20
    Date 와 Time 소개  (0) 2022.05.15
    Optional API 사용  (0) 2022.04.17
    [더 자바8] Opitonal 소개  (0) 2022.04.05
Designed by Tistory.