지금 이순간을 기계 시간으로 표현하는 방법
Instant instatnt = Instant.now();
System.out.println(instatnt);
System.out.println(instatnt.atZone(ZoneId.of("Asia/Seoul")));
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId); //Asia/Seoul
ZonedDateTime zonedDateTime = instatnt.atZone(ZoneId.systemDefault());
System.out.println(zonedDateTime);
//2020-10-06T13:05:33.380655+09:00[Asia/Seoul]
localDateTime.now()가 서버에 올라가게 되면 서버의 ZoneId()를 참고해서 시간을 리턴한다, 만약 서버가 미국에 있다면 미국시간을 리턴
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime birthday = LocalDateTime.of(1995, Month.NOVEMBER, 9, 0, 0, 0);
System.out.println(birthday);
ZonedDateTime nowInKorea = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
System.out.println(nowInKorea);
ZonedDateTime of = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC"));
System.out.println(of);
Instant nowInstant = Instant.now();
ZonedDateTime zonedDateTime1 = nowInstant.atZone(ZoneId.of("Asia/Seoul"));
System.out.println(zonedDateTime1);
//출력
2020-10-06T13:46:54.628380+09:00[Asia/Seoul]
2020-10-06T13:46:54.628517Z[UTC]
2020-10-06T13:46:54.628839+09:00[Asia/Seoul]
between
LocalDate today = LocalDate.now();
LocalDate thisYearBirthday = LocalDate.of(2020, Month.NOVEMBER, 9);
Period period = Period.between(today, thisYearBirthday);
System.out.println(period); //1M3D month와 day
System.out.println(period.getDays()); // 날짜만(month는 짤린다) '3' 만 출력
until
Period untilBirthday = today.until(thisYearBirthday);
System.out.println(untilBirthday);
System.out.println(untilBirthday.get(ChronoUnit.DAYS));
//출력값은 위 btween과 동일하게 나온다
P1M3D
3
Instant nowDuration = Instant.now();
Instant plus10 = nowDuration.plus(10, ChronoUnit.SECONDS);
Duration between = Duration.between(nowDuration, plus10);
System.out.println(between.getSeconds());
//출력
10
Instant plus10d = nowDuration.plus(10, ChronoUnit.DAYS);
Duration betweenDay = Duration.between(nowDuration, plus10d);
System.out.println(betweenDay.get(ChronoUnit.SECONDS));
System.out.println(betweenDay.getSeconds());
//출력
864000
864000
LocalDateTime nowDateTime = LocalDateTime.now();
//formatter 설정
DateTimeFormatter MMddyyyy = DateTimeFormatter.ofPattern("MM/dd/yyyy");
System.out.println(now.format(MMddyyyy));
//출력
10/06/2020
//MMddyyyy 는 DateTimeFormatter이다
LocalDate parse = LocalDate.parse("11/09/1995", MMddyyyy);
System.out.println(parse.format(MMddyyyy));
System.out.println(parse);
//output
11/09/1995
1995-11-09
Date date = new Date();
//Date -> Instant
Instant instant = date.toInstant();
//Instant -> Date
Date newDate = Date.from(instant);
System.out.println(instant.atZone(zoneId.systemDefault()));
System.out.println(newDate.getTime());
//output
2020-10-06T14:34:49.726+09:00[Asia/Seoul]
1601962489726
GregorianCalendar gregorianCalendar = new GregorianCalendar();
//그레고리력 타입 -> LocalDateTime
LocalDateTime dateTime = gregorianCalendar.toInstant().atZone(ZoneId.systemDefault())
.toLocalDateTime();
System.out.println(dateTime);
//output
2020-10-06T14:34:49.770
ZonedDateTime zonedDateTime2 = gregorianCalendar.toInstant().atZone(zoneId.systemDefault());
//ZonedDateTime -> 그레고리력 타입
GregorianCalendar from = GregorianCalendar.from(zonedDateTime2);
System.out.println(from.getTime());
//output
Tue Oct 06 14:34:49 KST 2020
ZoneId zoneId1 = TimeZone.getTimeZone("PST").toZoneId();
TimeZone timeZone = TimeZone.getTimeZone(zoneId1);
System.out.println(zoneId1.getId());
System.out.println(timeZone.getID());
//output
America/Los_Angeles
America/Los_Angeles
LocalDateTime의 immutable한 특징으로 기존시간에 시간을 더하더라도 기존 시간에는 영향이 없고 새로운 인스턴스를 만들어서 반환한다. 따라서 반환된 값을 사용
LocalDateTime nowTest = LocalDateTime.now();
LocalDateTime plus = nowTest.plus(10, ChronoUnit.HOURS);