JAVA 8 (2) 날짜 및 시간 API

신복호·2020년 8월 30일
0

java

목록 보기
2/3
post-thumbnail

날짜 및 시간 API 추가

기존 API

  • java.util.Date : 날짜와 시간에 관한 정보를 나타내는 클래스 (UTC 사용)
  • java.util.Calendar : 1970년 1월 1일 기준 , 날짜와 시간 -> 날짜를 조작하는데 있어 Date에 비해 더 많은 메소드를 제공하는 클래스
  • java.text.simpleDateFormat : SimpleDateFormat은 로케일 구분 방식으로 날짜를 형식화하고 구문 분석하기위한 구체적인 클래스

출처 : https://docs.oracle.com/javase/7/docs/api/java/util/Date.html

신규 API

  • java.time.LocalDate : 달력 시스템에서 시간대가 없는 날짜만을 표기하는 클래스 (UTC x)
  • java.time.LocalTime : 달력 시스템에서 시간대를 표기하는 클래스 (UTC x)
  • java.time.LocalDateTime : 날짜와 시간을 모두 표기 할때 사용되는 클래스
  • java.time.ZonedDateTime : ISO-8601 달력 시스템에서 정의하고 있는 타임존의 날짜와 시간을 저장하는 클래스
  • java.time.DateTimeFormatter : java.time에 대한 형식 (날짜 -> 텍스트), 변환 (텍스트 -> 날짜)을 나타내는 클래스
  • java.time.Duration : 시간을 초 단위 및 나노초 단위로 측정하는 클래스
  • java.time.Period : 시간을 년,월,일로 측정하는 클래스
  • java.time.TemporalAdjuster :소 현재 날짜를 기준으로 년도의 첫 번째 일, 마지막 일, 월의 첫 번째 일, 마지막일, 지난 요일 및, 돌아오는 요일 등 상대적인 날짜로 변경하게 하는 클래스

출처 : https://docs.oracle.com/javase/8/docs/api/

LocalDate


        LocalDate currentDate = LocalDate.now();
        LocalDate temporalDate = LocalDate.of(2020,02,03);

        System.out.println("현재 날짜 ::: " + currentDate);
        System.out.println("임시 날짜 ::: " + temporalDate);
 

LocalTime

        LocalTime currentTime = LocalTime.now();
        LocalTime temporalTime =LocalTime.of(21,12,32,22);
        
        System.out.println("현재 시간 ::: " +  currentTime);
        System.out.println("임의 시간 ::: " + temporalTime);

LocalDateTime


        LocalDateTime currentDateTime = LocalDateTime.now();
        LocalDateTime temporalDateTime = LocalDateTime.of( 2020,  12,  12,  12,  12);

        System.out.println("현재 날짜시간 :::" + currentDateTime);
        System.out.println("임시 날짜시간 ::: " + temporalDateTime);
    

ZonedDateTime


       ZonedDateTime UTC = ZonedDateTime.now(ZoneId.of("UTC"));
       ZonedDateTime SEOUL =ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
       ZoneOffset seoulZoneOff = ZoneOffset.ofHours(9); // 북미처럼 summer time 이 적용되는 곳에서 날짜별로 지정 가능


       System.out.println("UTC :::" + UTC);
       System.out.println("Seoul :::" + SEOUL);
       System.out.println("시차 임의 지정 ::: " + ZonedDateTime.now(seoulZoneOff));

DateTimeFormatter


        LocalDateTime currentDateTime = LocalDateTime.now();

        System.out.println("날짜 표기 :::"  + currentDateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
        System.out.println("날짜 표기2 :::"  + currentDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));

Duration


 	System.out.println(  Duration.between(LocalTime.of(10, 50), LocalTime.of(19, 0))); // (19시00분00초 - 10시50분00초) = 8시간10분간(PT8H10M));
        System.out.println(  Duration.between(LocalDateTime.of(2015, 1, 1, 0, 0), LocalDateTime.of(2016, 1, 1, 0, 0)).toDays());

Period

        System.out.println( Period.ofYears(2)); 
        System.out.println( Period.ofMonths(5)); 
        System.out.println( Period.ofWeeks(3)); 
        System.out.println("날짜 차이 :::" +  Period.between(LocalDate.of(1945, 8, 15), LocalDate.of(2020, 8, 29)));

TemporalAdjuster


        LocalDate localDate = LocalDate.of(2017, 07, 8);
        LocalDate nextSunday = localDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));

        System.out.println("현재 날짜 ::: " + localDate);
        System.out.println("다음 일요일 ::: " + nextSunday);

profile
한참 열정이 가득한 백엔드 개발자입니다.

0개의 댓글