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 2021Date๊ฐ์ฒด๋ฅผ ๋ณด๋ฉด setTime()๊ณผ ๊ฐ์ ๋ฉ์๋๋ก ๋ด๋ถ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ mutableํ์ฌ ๋ฉํฐ์ค๋ ๋ ํ๊ฒฝ์ ์ ํฉํ์ง ์๋ค.
-
ํด๋์ค ์ด๋ฆ์ด ๋ช ํํ์ง ์๋ค. (Date์ธ๋ฐ ์๊ฐ๊น์ง ๋ค๋ฃฌ๋ค.) Date ๋ผ๋ ํด๋์ค์ธ๋ฐ getTime()๊ณผ ๊ฐ์ด ์๊ฐ๊น์ง ๋ค๋ฃฌ๋ค.
-
ํ์ ์์ ์ฑ์ด ์๊ณ ์์ด 0๋ถํฐ ์์ํ๊ฑฐ๋ ๋ฑ๋ฑ ๋ฒ๊ทธ ๋ฐ์์ ์ฌ์ง๊ฐ ๋ง๋ค.
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๋ก ๋ฐ๊ธฐ ๋๋ฌธ์ ์ซ์๋ฉด ๋ชจ๋ ๋ค ๋ฐ์ ์ ์๊ณ ๊ทธ์ ๋ฐ๋ผ ์ด์ํ ๊ฐ์ด ์ฌ ์๊ฐ ์๋ค.
- java.util.Date
- java.util.Calendar
- java.text.SimpleDateFormat
- 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.Duration
- java.time.Period
- java.time.TemporalAdjuster
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 ๊ธฐ์ค)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); //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);DateTimeFormatter์ ofPattern()์ ์ด์ฉํด ํน์ ํจํด์ ์ง์ ํ ์ ์๊ณ ๋ฏธ๋ฆฌ ์ ์๋ ํฌ๋งทํฐ๋ค์ด ์กด์ฌํ๋๋ฐ ์ด ํ์์ ์ด์ฉํ๊ณ ์ํ๋ฉด ๊ตณ์ด ์๋ก ์ ์ํด์ค ํ์๊ฐ ์๋ค. ์ ์๋ ํฌ๋งทํฐ๋ค์ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ
๋ Date/Timeํ์ ์ parse()๋ฉ์๋๋ฅผ ํตํด์ ํน์ ๋ฌธ์์ด์ Date/Time ํ์ ์ผ๋ก ํ์ฑํ ์ ์๋ค.
//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()); //10Period๋ ์ฌ๋์ด ์ฌ์ฉํ๋ ๋ ์ง/์๊ฐ์ ๊ธฐ๊ฐ์ ์ธก์ , Duration์ ์ด๋จ์(๋๋ ธ,๋ฐ๋ฆฌ)๋ก ๋ฐํ์ ํ๊ธฐ ๋๋ฌธ์ ์ฃผ๋ก ๊ธฐ๊ณ์ฉ ์๊ฐ๊ฐ์ ๊ธฐ๊ฐ์ ์ธก์ ํ๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
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ํ ์์ฑ์ ๊ฐ๊ณ ์ค๊ณ๊ฐ ๋์๋๋ฐ ์ด ๋๋ฌธ์ ๋ฉ์๋๋ฅผ ์ด์ฉํด ๋ ์ง,์๊ฐ์ ๋ณ๊ฒฝํ๋ฉด ์์ ์ ์๋ ํจ์์ฒ๋ผ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ๋ฐํํ๊ณ ์๋ค.
https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/