-
JAVA 8 : Date์ TimeJAVA 2021. 3. 21. 19:48๋ฐ์ํ
์๋ฐ 8์ ์๋ก์ด ๋ ์ง์ API๊ฐ ์๊ธด ์ด์ ?
- ์ด์ ๊น์ง ์ฌ์ฉํ๋ java.util.Dateํด๋์ค๋ mutable(๊ฐ๋ณ๊ฐ์ฒด, ์์ฑํ์๋ ์ํ๋ฅผ ๋ฐ๊ฟ ์ ์๋ ๊ฐ์ฒด๊ธฐ ๋๋ฌธ์ ๊ฐ์ ๊ฐ์ฒด์ฌ๋ ๋ค๋ฅธ ๊ฐ์ ๋ผ ์ ์์)ํ๊ธฐ ๋๋ฌธ์ ๋ฉํฐ ์ฐ๋ ๋ ํ๊ฒฝ์์ ์์ ํ๊ฒ ์ฐ์ด๊ธฐ ์ด๋ ค์
- ํด๋์ค์ ๋ชจํธํ ์๋ช , Date ์ธ๋ฐ ๊ฒฐ๊ณผ๊ฐ์ ์ถ๋ ฅํด๋ณด๋ฉด ์๊ฐ๊น์ง ๋ค๋ฃธ
- ๋ฒ๊ทธ ๋ฐ์์ ์ฌ์ง๊ฐ ๋ง์ (month๊ฐ 0๋ถํฐ ์์ํ๊ธฐ์ ์ซ์๋ก ์ธ ๊ฒฝ์ฐ ์ค์ํ ์ฌ์ง๊ฐ ๋ง๊ณ , ํ์ ์์ ์ฑ์ด ์์)
- ์ด๋ฌํ ๋ฌธ์ ๋๋ฌธ์ ๋ ์ง ์๊ฐ ์ฒ๋ฆฌ๊ฐ ๋ณต์กํ ์ ํ๋ฆฌ์ผ์ด์ ์ Joda Time์ ์ผ์
* java.util.Date ํด๋์ค์ ๋ฌธ์ ์ ์ ๋ํด ์ ํ์๋ ๋ถ๋ถ์ด ์์ผ๋ ์ฝ์ด๋ณด๊ธธ
All about java.util.Date
This post is an attempt to reduce the number of times I need to explain things in Stack Overflow comments. You may well be reading it via a link from Stack Overflow – I intend to refer to thi…
codeblog.jonskeet.uk
์๋ฐ 8์์ ์ ๊ณตํ๋ Date-Time API
- JSR-310 ์คํฉ์ ๊ตฌํ์ฒด๋ฅผ ์ ๊ณต
- ๋์์ธ ์ฒ ํ
Clear
Fluent
Immutable (๋ถ๋ณ)
Extensible
์ฃผ์ API
- ๊ธฐ๊ณ์ฉ ์๊ฐ(machine Time)๊ณผ ์ธ๋ฅ์ฉ ์๊ฐ(human Time)์ผ๋ก ๋๋ ์ ์์
- ๊ธฐ๊ณ์ฉ ์๊ฐ์ EPOCK (1970๋ 1์ 1์ผ 0์ 0๋ถ 0์ด)๋ถํฐ ํ์ฌ๊น์ง์ ํ์์คํฌํ๋ฅผ ํํํจ
- ์ธ๋ฅ์ฉ ์๊ฐ์ ์ฐ๋ฆฌ๊ฐ ํํ ์ฌ์ฉํ๋ ๋ , ์, ์ผ, ์, ๋ถ, ์ด ๋ฑ์ ํํ
- ํ์์คํฌํ๋ Instant๋ฅผ ์ฌ์ฉ
- ํน์ ๋ ์ง(LocalDate), ์๊ฐ(LocalTime), ์ผ์(LocalDateTime)๋ฅผ ์ฌ์ฉํ ์ ์์
- ๊ธฐ๊ฐ์ ํํํ ๋๋ Duration(์๊ฐ ๊ธฐ๋ฐ)๊ณผ Period(๋ ์ง ๊ธฐ๋ฐ)๋ฅผ ์ฌ์ฉํ ์ ์์
- DateTimeFomatter๋ฅผ ์ฌ์ฉํด์ ์ผ์๋ฅผ ํน์ ํ ๋ฌธ์์ด๋ก ํฌ๋งทํ ํ ์ ์์
* Date Time ๊ด๋ จ ๊ณต์ ๋ฌธ์๋ค
Lesson: Date-Time Overview (The Java™ Tutorials > Date Time)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
Overview (The Java™ Tutorials > Date Time > Standard Calendar)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated
docs.oracle.com
Date์ Time API
ํ์ฌ ์๊ฐ์ ๊ธฐ๊ณ ์๊ฐ์ผ๋ก ํํํ๋ ๋ฐฉ๋ฒ
- Instatnt.now() : ํ์ฌ UTC (Universal Time Coordinated, GMT(Greenwich Mean Time))๋ฅผ ๋ฆฌํดํจ
์ธ๋ฅ์ฉ ์ผ์๋ฅผ ํํํ๋ ๋ฐฉ๋ฒ
- LocalDateTime.now() : ํ์ฌ ์์คํ Zone์ ํด๋น(Local)ํ๋ ์ผ์๋ฅผ ๋ฆฌํดํจ
- LocalDateTime.of(int, Month, int, int, int, int, ZoneID): ํน์ Zone์ ํน์ ์ผ์๋ฅผ ๋ฆฌํดํจ
public class DateTimeTest {public static void main(String[] args){LocalDateTime now = LocalDateTime.now();System.out.println(now); //์๋ฒ์ zone์ ๋ณด๋ฅผ ์ฐธ๊ณ ํด ๊ทธ ์๊ฐ์ด ์ฐ์ธ๋ค๋ ์ ์ ์ฃผ์ !LocalDateTime birthday = LocalDateTime.of(1993, Month.APRIL, 15, 0,0,0);System.out.println(birthday);//ํ์ฌ ํด๋นํ๋ zone์ ๋ ์ง๋ฅผ ์ถ๋ ฅ (ZonedDateTime๊ณผ Instant ๋ณํํด๊ฐ๋ฉฐ ์ธ ์ ์์)ZonedDateTime nowInKorea = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));System.out.println(nowInKorea);Instant nowInstant = Instant.now();ZonedDateTime zonedDateTime = nowInstant.atZone(ZoneId.of("Asia/Seoul"));System.out.println(zonedDateTime);}}cs ๊ธฐ๊ฐ์ ํํํ๋ ๋ฐฉ๋ฒ
- Period / Duration.between()
- period๋ ํด๋จผ์ฉ ๊ธฐ๊ฐ, Duration์ ๋จธ์ ์ฉ ๊ธฐ๊ฐ์ ๋น๊ต
public class DateTimeTest {public static void main(String[] args){//PeriodLocalDate today = LocalDate.now();LocalDate thisYearBirthday = LocalDate.of(2021, Month.MARCH, 20);Period period = Period.between(today, thisYearBirthday);System.out.println(period.getDays());Period until = today.until(thisYearBirthday);System.out.println(until.get(ChronoUnit.DAYS));//DurationInstant now = Instant.now();Instant plus = now.plus(10,ChronoUnit.SECONDS);Duration between = Duration.between(now, plus);System.out.println(between.getSeconds());}}cs ํ์ฑ ๋๋ ํฌ๋งคํ
- LocalDateTime.parse(String, DateTimeFormatter);
- Datetime
public class DateTimeTest {public static void main(String[] args){LocalDateTime date = LocalDateTime.now();System.out.println(date); // ์ถ๋ ฅ ๊ฒฐ๊ณผ : 2021-03-21T19:18:43.252//ํฌ๋งคํLocalDateTime now = LocalDateTime.now();DateTimeFormatter MMddyyyy = DateTimeFormatter.ofPattern("MM/dd/yyyy");System.out.println(now.format(MMddyyyy)); //์ถ๋ ฅ ๊ฒฐ๊ณผ : 2021-03-21//ํ์ฑLocalDate parse = LocalDate.parse("12/24/2021", MMddyyyy);System.out.print(parse);}}cs * ๊ธฐ์กด์ ๋ฏธ๋ฆฌ ์ ์ํด๋ ํฌ๋งท ์ฐธ๊ณ
DateTimeFormatter (Java Platform SE 8 )
Parses the text using this formatter, without resolving the result, intended for advanced use cases. Parsing is implemented as a two-phase operation. First, the text is parsed using the layout defined by the formatter, producing a Map of field to value, a
docs.oracle.com
๋ ๊ฑฐ์ API ์ง์- GregorianCalendar์ Date ํ์ ์ ์ธ์คํด์ค๋ฅผ Instant๋ ZonedDateTime์ผ๋ก ๋ณํ
- java.util.TimeZone์์ java.time.ZoneId๋ก ์ํธ ๋ณํ ๊ฐ๋ฅ
public class DateTimeTest {public static void main(String[] args){//date์ instant ๋ณํDate date = new Date();Instant instant = date.toInstant();Date newDate = Date.from(instant);//GregorianCalendar LocalDateTime์ผ๋ก ๋ณํGregorianCalendar gregorianCalendar = new GregorianCalendar();ZonedDateTime dateTime = gregorianCalendar.toInstant().atZone(ZoneId.systemDefault());GregorianCalendar from = GregorianCalendar.from(dateTime);//TimeZoneZoneId zoneId = TimeZone.getTimeZone("PST").toZoneId();TimeZone timeZone = TimeZone.getTimeZone(zoneId);}}cs * ChronoUnit ํ์ฉ
ChronoUnit์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ๋ ๊ฐ๋จํ๊ฒ ํน์ ์๊ฐ ๋จ์๋ก ์๊ฐ์ ๊ธธ์ด๋ฅผ ๊ตฌํ ์ ์์
ChronoUnit (Java Platform SE 8 )
Returns a copy of the specified temporal object with the specified period added. The period added is a multiple of this unit. For example, this method could be used to add "3 days" to a date by calling this method on the instance representing "days", passi
docs.oracle.com
๋ฐ์ํ'JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ JAVA ] Object Mapper : json ์ง๋ ฌํ, ์ญ์ง๋ ฌํ ์ฌ์ฉ๋ฒ (0) 2022.03.17 [ JAVA ] ์ง๋ ฌํ Serializable , ์ญ์ง๋ ฌํ Deserialize (0) 2022.03.17 JAVA 8 : Optional (0) 2021.03.18