์๋ก ์ถ๊ฐ ๋ ์ด์
1. java.util.Date ํด๋์ค๋ mutableํ์ฌ thread safe ํ์ง ์๋ค.
Copy Date date = new Date();
long time = date.getTime();
System.out.println(date); //Tue Nov 02 17:34:03 KST 2021
Thread.sleep();
Date date2 = new Date();
System.out.println(date2); //Tue Nov 02 17:34:06 KST 2021
date2.setTime(time);
System.out.println(date2); //Tue Nov 02 17:34:03 KST 2021
Date๊ฐ์ฒด๋ฅผ ๋ณด๋ฉด setTime()๊ณผ ๊ฐ์ ๋ฉ์๋๋ก ๋ด๋ถ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ mutableํ์ฌ ๋ฉํฐ์ค๋ ๋ ํ๊ฒฝ์ ์ ํฉํ์ง ์๋ค.
ํด๋์ค ์ด๋ฆ์ด ๋ช
ํํ์ง ์๋ค. (Date์ธ๋ฐ ์๊ฐ๊น์ง ๋ค๋ฃฌ๋ค.) Date ๋ผ๋ ํด๋์ค์ธ๋ฐ getTime()๊ณผ ๊ฐ์ด ์๊ฐ๊น์ง ๋ค๋ฃฌ๋ค.
ํ์
์์ ์ฑ์ด ์๊ณ ์์ด 0๋ถํฐ ์์ํ๊ฑฐ๋ ๋ฑ๋ฑ ๋ฒ๊ทธ ๋ฐ์์ ์ฌ์ง๊ฐ ๋ง๋ค.
Copy Date date = new Date(-10000,1,1,1,1);
System.out.println(date); //Thu Feb 01 01:01:00 KST 8101
//Date
public Date(int year, int month, int date, int hrs, int min) {
this(year, month, date, hrs, min, 0);
}
ํ์
์ int๋ก ๋ฐ๊ธฐ ๋๋ฌธ์ ์ซ์๋ฉด ๋ชจ๋ ๋ค ๋ฐ์ ์ ์๊ณ ๊ทธ์ ๋ฐ๋ผ ์ด์ํ ๊ฐ์ด ์ฌ ์๊ฐ ์๋ค.
ํน์ง
๊ธฐ์กด API
java.text.SimpleDateFormat
์ถ๊ฐ๋ API
java.time.Instant : ๊ธฐ๊ณ์๊ฐ
java.time.LocalDate : ๋ ์ง(์๊ฐx), ํ์์กดx
java.time.LocalTime : ์๊ฐ(๋ ์งx), ํ์์กดx
java.time.LocalDateTime : ๋ ์ง/์๊ฐ, ํ์์กดx
java.time.ZonedDateTime : ๋ ์ง/์๊ฐ, ํ์์กดo
java.time.DateTimeFormatter
java.time.TemporalAdjuster
์ฌ์ฉ ์
1. Instant
Copy Instant instant = Instant.now();
System.out.println(instant); //2021-11-02T14:07:19.218304100Z (๊ธฐ์ค์ UTC ๊ธฐ์ค)
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
System.out.println(instant); //2021-11-02T23:08:45.188601700+09:00[Asia/Seoul] (ํ์ฌ KTC ๊ธฐ์ค)
2. LocalDate/LocalTime/LocalDateTime/ZonedDateTime
Copy LocalDateTime now = LocalDateTime.now(); //์๋ฒ์ ์์คํ
zone ๊ธฐ์ค
System.out.println(now);
LocalDateTime of = LocalDateTime.of(1982, Month.JULU, 15,0,0,0);
ZonedDateTime nowInKorea = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
System.out.println(nowInKorea);
Copy //formatting
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
System.out.println(now.format(formatter)); //2021-11-02T23:28:51.388655
formatter = DateTimeFormatter.ofPattern("MM.dd.yyyy");
System.out.println(now.format(formatter)); //11.02.2021
//parsing
LocalDate parse = LocalDate.parse("08.12.2021",formatter);
System.out.println(parse);
๋ Date/Timeํ์
์ parse()๋ฉ์๋๋ฅผ ํตํด์ ํน์ ๋ฌธ์์ด์ Date/Time ํ์
์ผ๋ก ํ์ฑํ ์ ์๋ค.
4. Duration / Period
Copy //Period
LocalDate today = LocalDate.now();
LocalDate thisYearBirthDay = LocalDate.of(2022, Month.FEBRUARY,7);
Period period = Period.between(today, thisYearBirthDay);
System.out.println("์์ผ๊น์ง ๋จ์ ๊ธฐ๊ฐ : " + period.getYears() + " ๋
" + period.getMonths() + "์ " + period.getDays() + "์ผ" ); //์์ผ๊น์ง ๋จ์ ๊ธฐ๊ฐ : 0 ๋
3์ 5์ผ
Period p = today.until(thisYearBirthDay);
System.out.println("์์ผ๊น์ง ๋จ์ ๊ธฐ๊ฐ : " + p.getYears() + " ๋
" + p.getMonths() + "์ " + p.getDays() + "์ผ" ); //์์ผ๊น์ง ๋จ์ ๊ธฐ๊ฐ : 0 ๋
3์ 5์ผ
//Duration
Instant now = Instant.now();
Instant plus = now.plus(10,ChronoUnit.SECONDS);
Duration between = Duration.between(now,plus);
System.out.println(between.getSeconds()); //10
Period๋ ์ฌ๋์ด ์ฌ์ฉํ๋ ๋ ์ง/์๊ฐ์ ๊ธฐ๊ฐ์ ์ธก์ , Duration์ ์ด๋จ์(๋๋
ธ,๋ฐ๋ฆฌ)๋ก ๋ฐํ์ ํ๊ธฐ ๋๋ฌธ์ ์ฃผ๋ก ๊ธฐ๊ณ์ฉ ์๊ฐ๊ฐ์ ๊ธฐ๊ฐ์ ์ธก์ ํ๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
์ถ๊ฐ
Immutable ํ Date/Time
Copy LocalDateTime now = LocalDateTime.now();
Lonow = now.plusDays(1);
//LocalDateTime์ plusDays() ๋ฉ์๋
public LocalDateTime plusDays(long days) {
LocalDate newDate = date.plusDays(days);
return with(newDate, time);
}
private LocalDateTime with(LocalDate newDate, LocalTime newTime) {
if (date == newDate && time == newTime) {
return this;
}
return new LocalDateTime(newDate, newTime);
}
Date/Time์ ๋ชจ๋ ๊ฐ์ฒด๋ mutableํ ์์ฑ์ ๋จ์ ์ ํด๊ฒฐํ๊ณ ์ Immutableํ ์์ฑ์ ๊ฐ๊ณ ์ค๊ณ๊ฐ ๋์๋๋ฐ ์ด ๋๋ฌธ์ ๋ฉ์๋๋ฅผ ์ด์ฉํด ๋ ์ง,์๊ฐ์ ๋ณ๊ฒฝํ๋ฉด ์์ ์ ์๋ ํจ์์ฒ๋ผ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋ฐํํ๊ณ ์๋ค.
Reference
https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/