JAVA

JAVA 8 : Date์™€ Time

PYT 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, 150,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){
 
        //Period
        LocalDate 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));
 
 
        //Duration
        Instant 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);
        
        //TimeZone 
        ZoneId 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

 

๋ฐ˜์‘ํ˜•