jdk8의datetime 시간 함수 사용 예시

4894 단어 jdk8datetime시간
JDK8이 발표되었습니다.datetime 시간 함수 사용 방법의 작은 예시를 썼습니다

package datetime;

import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
import static java.time.temporal.TemporalAdjusters.previousOrSame;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class DataTimes {

 public static void main(String[] args) {
  //
  LocalDateTime timePoint = LocalDateTime.now(); //
  System.out.println("-- ----");
  System.out.println(timePoint);
  System.out.println("");

  System.out.println("-- ----");
  System.out.println(LocalDate.of(2012, Month.DECEMBER, 12)); // from
                 // values
  System.out.println(LocalDate.ofEpochDay(150)); // middle of 1970
  System.out.println(LocalTime.of(17, 18)); // the train I took home today
  System.out.println(LocalTime.parse("10:15:30")); // From a String
  System.out.println("");

  System.out.println("-- ----");
  System.out.println(timePoint.toLocalDate());
  System.out.println(timePoint.getMonth());
  System.out.println(timePoint.getDayOfMonth());
  System.out.println(timePoint.getSecond());
  System.out.println("");

  System.out.println("--- ---");
  LocalDateTime thePast = timePoint.withDayOfMonth(10).withYear(2010);
  System.out.println(thePast);
  System.out.println("--- 3 ---");
  LocalDateTime yetAnother = thePast.plusWeeks(3).plus(3,
    ChronoUnit.WEEKS);
  System.out.println(yetAnother);
  System.out.println("");

  System.out.println("--- ---");
  System.out.println(timePoint);
  System.out.println(timePoint.with(lastDayOfMonth()));
  System.out.println(timePoint.with(previousOrSame(DayOfWeek.WEDNESDAY)));
  System.out.println(timePoint.with(LocalTime.now()));
  System.out.println("");

  System.out.println("--- --");
  System.out.println(timePoint);
  LocalDateTime truncatedTimeToMinutes = timePoint
    .truncatedTo(ChronoUnit.MINUTES);
  System.out.println(truncatedTimeToMinutes);
  LocalDateTime truncatedTimeToSeconds = timePoint
    .truncatedTo(ChronoUnit.SECONDS);
  System.out.println(truncatedTimeToSeconds);
  System.out.println("");

  System.out.println("--- ---");
  ZonedDateTime zoned_now = ZonedDateTime.of(LocalDateTime.now(),
    ZoneId.of("UTC+08:00"));
  System.out
    .println(zoned_now.withZoneSameInstant(ZoneId.of("UTC+00:00")));
  System.out.println(zoned_now.getOffset());
  System.out.println("");

  System.out.println("--- ---");
  OffsetTime time = OffsetTime.now();
  ZoneOffset offset = ZoneOffset.of("+02:00");
  System.out.println(time);
  System.out.println(time.withOffsetSameInstant(offset));
  System.out.println("");

  System.out.println("--- ---");
  timePoint = LocalDateTime.now();
  System.out.println(timePoint);
  // 3 years, 2 months, 1 day
  Period period1 = Period.of(3, 2, 1);
  System.out.println(timePoint.plus(period1));
  Duration duration = Duration.ofSeconds(3, 5);
  System.out.println(timePoint.plus(duration));
  Duration sixHours = Duration.between(
    ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+08:00")),
    ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+02:00")));
  System.out.println(timePoint.plus(sixHours));
  System.out.println("");

 }
}

실행 결과

-- ----
2014-03-25T17:03:40.553

-- ----
2012-12-12
1970-05-31
17:18
10:15:30

-- ----
2014-03-25
MARCH
25
40

--- ---
2010-03-10T17:03:40.553
--- 3 ---
2010-04-21T17:03:40.553

--- ---
2014-03-25T17:03:40.553
2014-03-31T17:03:40.553
2014-03-19T17:03:40.553
2014-03-25T17:03:40.583

--- --
2014-03-25T17:03:40.553
2014-03-25T17:03
2014-03-25T17:03:40

--- ---
2014-03-25T09:03:40.583Z[UTC]
+08:00

--- ---
17:03:40.585+08:00
11:03:40.585+02:00

--- ---
2014-03-25T17:03:40.585
2017-05-26T17:03:40.585
2014-03-25T17:03:43.585000005
2014-03-25T23:03:40.586

좋은 웹페이지 즐겨찾기