java.time
ํจํค์ง
// ์ค๋๋ ์ง / ์๊ฐ / ๋ ์ง ๋ฐ ์๊ฐ
LocalDate nowDate = LocalDate.now()
LocalTime nowTime = LocalTime.now()
LocalDateTime nowDateTime = LocalDateTime.now();
// ๋ ์ง ์ง์ ์ง์
LocalDate date = LocalDate.of(2021, 10, 26);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy๋
MM์ dd์ผ");
String nowDate1 = formatter1.format(LocalDate.now());
String nowDate2 = formatter2.format(LocalDate.now());
System.out.println("nowDate1 = " + nowDate1);
System.out.println("nowDate2 = " + nowDate2);
์ง์ ์ ์ธ ์ฐ์ฐ์ด ์๋๊ณ ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ ๊ต์ฅํ ๋ช ์์ ์ด์ด์ ์ค์? ์ ์ฌ์ง๊ฐ ์์ ๊ฒ ๊ฐ๋ค. ํด๋์ค ์ด๋ฆ์ ๋๋ ๊ธฐ์ตํ๊ณ ์ ์ฉํด๋ณด์ ใ
// 2021/10/26
LocalDate now = LocalDate.now();
LocalDate pastDate = LocalDate.of(2021,1,1);
// Period period = Period.between(์์๋ ์ง, ๋๋ ์ง);
Period period = Period.between(pastDate, now);
// pastDate๋ก๋ถํฐ now๊ฐ ์ผ๋ง๋ ์ง๋ฌ๋์ง ํ์ธ
System.out.println(period.getMonths()); // ๋ช ๋ฌ์ด ์ง๋ฌ๋์ง
System.out.println(period.getDays()); // ๋ช ์ผ์ด ์ง๋ฌ๋์ง
java.time
ํจํค์ง๋ฅผ ์ด์ฉํด์ ๋ง๋ ๋ ์ง/์๊ฐ ๊ฐ์ฒด๋ ๋ถ๋ฑํธ๋ฅผ ์ด์ฉํ ๋์ ๋น๊ต๊ฐ ์ ๋จ.isAfter
, isEqual
, isBefore
๋ฉ์๋ ์ง์// 2021/10/26
LocalDate now = LocalDate.now();
LocalDate pastDate = LocalDate.of(2021,1,1);
// ์ด๊ฑฐ๋ True - now๊ฐ pastDate ์ดํ์ธ์ง
now.isAfter(pastDate);
// ์ด๊ฑฐ๋ False - now๊ฐ pastDate ์ด์ ์ธ์ง
now.isBefore(pastDate);