π¦ chap12. μλ‘μ΄ λ μ§μ μκ° API
chap12. μλ‘μ΄ λ μ§μ μκ° API
12.1 LocalDate, LocalTime, Instant, Duration, Period ν΄λμ€
12.1.1 LocalDateμ LocalTime μ¬μ©
LocalDate μΈμ€ν΄μ€λ μκ°μ μ μΈν λ μ§λ₯Ό νννλ λΆλ³ κ°μ²΄λ€.
1
2
3
4
5
6
7
LocalDate date = LocalDate.of(2017, 9, 21);
int year = date.getYear();
Month month = date.getMonth();
int day = date.getDayOfMonth();
DayOfWeek dow = date.getDayOfWeek();
int len = date.lengthOfMonth();
boolean leap = date.isLeapYear();
ν©ν 리 λ©μλ now λ μμ€ν μκ³μ μ 보λ₯Ό μ΄μ©ν΄μ νμ¬ λ μ§ μ 보λ₯Ό μ»λλ€.
1
LocalDate today = LocalDate.now();
get λ©μλμ TemporalField μΈν°νμ΄μ€λ₯Ό μ λ¬ν΄μ μ 보λ₯Ό μ»λ λ°©λ²λ μλ€.
μ΄κ±°μ ChronoField λ TemporalField μΈν°νμ΄μ€λ₯Ό μ μνλ€.
1
2
3
int year = date.get(ChronoField.YEAR);
int month = date.get(ChronoField.MONTH_OF_YEAR);
int day = date.get(ChronoField.DAY_OF_MONTH);
λ΄μ₯ λ©μλλ₯Ό μ΄μ©ν΄ κ°λ μ±μ λμΌ μλ μλ€.
1
2
3
int year = date.getYear();
int month = date.getMonthValue();
int day = date.getDayOfMonth();
μκ°μ LocalTime ν΄λμ€λ‘ ννν μλ μλ€.
1
2
3
4
LocalTime time = LocalTime.of(13, 45, 20);
int hour = time.getHour();
int minute = time.getMinute();
int second = time.getSecond();
λ μ§μ μκ° λ¬Έμμ΄λ‘ parse μ μ λ©μλλ₯Ό νΈμΆν΄ μΈμ€ν΄μ€λ₯Ό λ§λ€ μλ μλ€.
1
2
LocalDate date = LocalDate.parse("2017-09-21");
LocalTime time = LocalTime.parse("13:45:20");
parse λ©μλμλ DateTimeFormatter λ₯Ό μ λ¬ν μλ μλ€.
12.1.2 λ μ§μ μκ° μ‘°ν©
LocalDateTime μ LocalDate μ LocalTime μ μμΌλ‘ κ°λ λ³΅ν© ν΄λμ€μ΄λ€.
1
2
3
4
5
LocalDateTime dt1 = LocalDateTime.of(2017, Month.SEPTEMBER, 21, 13, 45, 20);
LocalDateTime dt2 = LocalDateTime.of(date, time);
LocalDateTime dt3 = date.atTime(13, 45, 20);
LocalDateTime dt4 = date.atTime(time);
LocalDateTime dt5 = time.atDate(date);
LocalDateTime μμ κ° μΈμ€ν΄μ€λ₯Ό μΆμΆν μλ μλ€.
1
2
LocalDate date1 = dt1.toLocalDate();
LocalTime time1 = dt1.toLocalTime();
12.1.3 Instant ν΄λμ€ : κΈ°κ³μ λ μ§μ μκ°
κΈ°κ³μ κ΄μ μμλ μ°μλ μκ°μμ νΉμ μ§μ μ νλμ ν° μλ‘ νννλ κ²μ΄ κ°μ₯ μμ°μ€λ¬μ΄ μκ° νν λ°©μμ΄λ€.
java.time.Instant
ν΄λμ€μμλ κΈ°κ³μ μΈ κ΄μ μμ μκ°μ νννλ€.
μ΄λ μ λμ€ μν¬ν¬ μκ° Unix epoch time, 1970-01-01 00:00:00 UTC
μ κΈ°μ€μΌλ‘ νΉμ μμ κΉμ§μ μκ°μ μ΄λ‘ νννλ€.
1
2
3
4
Instant.ofEpochSecond(3);
Instant.ofEpochSecond(3, 0);
Instant.ofEpochSecond(2, 1_000_000_000);
Instant.ofEpochSecond(4, -1_000_000_000);
Instant ν΄λμ€λ μ¬λμ΄ νμΈν μ μλλ‘ μκ°μ νμν΄μ£Όλ μ μ ν©ν 리 λ©μλ now λ₯Ό μ 곡νλ€.
Instant λ μ΄μ λλ Έμ΄ μ 보λ₯Ό ν¬ν¨νλ€.
μ΄λ₯Ό μ½κΈ° μν΄ Duration κ³Ό Period ν΄λμ€λ₯Ό νμ©ν μ μλ€.
12.1.4 Duration κ³Ό Period μ μ
μ§κΈκΉμ§μ λͺ¨λ ν΄λμ€λ Temporal μΈν°νμ΄μ€λ₯Ό ꡬννλ€.
Duration ν΄λμ€μ μ μ ν©ν 리 λ©μλ between μΌλ‘ λ μκ° κ°μ²΄ μ¬μ΄μ μ§μμκ°μ λ§λ€ μ μλ€.
1
2
3
Duration d1 = Duration.between(time1, time2);
Duration d2 = Duration.between(dateTime1, dateTime2);
Duration d3 = Duration.between(instant1, instant2);
λ€λ§ LocalDateTime μ μ¬λμ΄ μ¬μ©νλλ‘, Instant λ κΈ°κ³κ° μ¬μ©νλλ‘ λ§λ€μ΄μ§ ν΄λμ€λ‘
λ μΈμ€ν΄μ€λ μλ‘ νΌν©ν μ μλ€.
λ , μ μΌλ‘ μκ°μ ννν λλ Period ν΄λμ€λ₯Ό μ¬μ©νλ€.
1
2
Period tenDays = Period.between(LocalDate.of(2017, 9, 11),
LocalDate.of(2017, 9, 21));
Duration κ³Ό Period ν΄λμ€λ μμ μ μΈμ€ν΄μ€λ₯Ό λ§λ€ μ μλλ‘ λ€μν ν©ν 리 λ©μλλ₯Ό μ 곡νλ€.
1
2
3
4
5
6
Duration threeMinutes = Duration.ofMinutes(3);
Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
Period tenDays = Period.ofDays(10);
Period threeWeeks = Period.ofWeeks(3);
Period twoYearsSixMonthsOnday = Period.of(2, 6, 1);
μ§κΈκΉμ§μ λͺ¨λ ν΄λμ€λ λΆλ³ ν΄λμ€μ΄λ€.
12.2 λ μ§ μ‘°μ , νμ±, ν¬λ§€ν
withArrtibute
λ©μλλ‘ κΈ°μ‘΄μ LocalDate λ₯Ό λ°κΎΌ λ²μ μ μ§μ κ°λ¨νκ² λ§λ€ μ μλ€.
1
2
3
4
LocalDate date1 = LocalDate.of(2017, 9, 21);
LocalDate date2 = date1.withYear(2011);
LocalDate date3 = date2.withDayOfMonth(25);
LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 2);
Temproal μΈν°νμ΄μ€λ LocalDate, LocalTime, LocalDateTime, Instant μ²λΌ νΉμ μκ°μ μ μνκ³
get κ³Ό with λ©μλλ‘ Temporal κ°μ²΄μ νλκ°μ μ½κ±°λ κ³ μΉ μ μλ€.
λ§μ½ μ§μ λ νλλ₯Ό μ§μνμ§ μμΌλ©΄ UnsupportedTemporalTypeException
μ΄ λ°μνλ€.
μ μΈνμΌλ‘ LocalDate λ₯Ό μ¬μ©νλ λ°©λ²λ μλ€.
1
2
3
4
LocalDate date1 = LcoalDate.of(2017, 9, 21);
LocalDate date2 = date1.plusWeeks(1);
LocalDate date3 = date2.minusYears(6);
LocalDate date4 = date3.plus(6, ChronoUnit.MONTHS);
plus, minus λ©μλλ Temporal μΈν°νμ΄μ€μ μ μλμ΄ μλ€.
μ΄λ₯Ό νμ©ν΄ νΉμ μκ°λ§νΌ μλ€λ‘ μ΄λμν¬ μ μλ€.
μ«μμ TemporalUnit μ μΈμλ‘ νμ©ν μ μλ€.
12.2.1 TemporalAdjusters μ¬μ©νκΈ°
TemporalAdjuster
λ with λ©μλμ μ’ λ λ€μν λμμ μνν μ μλλ‘ νλ κΈ°λ₯μ μ 곡νλ€.
1
2
3
4
5
import static java.time.tempoal.TemporalAdjusters.*;
LocalDate date1 = LocalDate.of(2014, 3, 18);
LocalDate date2 = date1.with(nextOfSame(DayOfWeek.SUNDAY));
LocalDate date3 = date2.with(lastDayOfMonth());
νμν κΈ°λ₯μ΄ μ μλμ΄ μμ§ μμ λλ λΉκ΅μ μ½κ² 컀μ€ν TemporalAdjuster ꡬνμ λ§λ€ μ μλ€.
1
2
3
4
@FunctionalInterface
public interface TemporalAdjuster {
Temporal adjustInto(Temporal temporal);
}
TemporalAdjuster μΈν°νμ΄μ€ ꡬνμ Temporal κ°μ²΄λ₯Ό μ΄λ»κ² λ€λ₯Έ Temporal κ°μ²΄λ‘ λ³νν μ§ μ μνλ€.
12.2.2 λ μ§μ μκ° κ°μ²΄ μΆλ ₯κ³Ό νμ±
ν¬λ§€ν
κ³Ό νμ± μ μ© ν¨ν€μ§μΈ java.time.format
μ΄ μΆκ°λμλ€.
κ°μ₯ μ€μν ν΄λμ€λ DateTimeFormatter
μ΄λ€.
μ΄λ₯Ό μ΄μ©ν΄μ λ μ§λ μκ°μ νΉμ νμμ λ¬Έμμ΄λ‘ λ§λ€ μ μλ€.
1
2
3
LocalDate date = LocalDate.of(2014, 3, 18);
String s1 = date.format(DateTimeFormatter.BASIC_ISO_DATE); // 20140318
String s2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE); // 2014-03-18
λ°λλ‘ λ¬Έμμ΄μ νμ±ν΄μ λ μ§ κ°μ²΄λ₯Ό λ€μ λ§λ€ μ μλ€.
1
2
LocalDate date1 = LocalDate.parse("20140318", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate date2 = LocalDate.parse("2014-03-18", DateTimeFormatter.ISO_LOCAL_DATE);
κΈ°μ‘΄ java.util.DateFormat
κ³Ό λ¬λ¦¬ λͺ¨λ DateTimeFormatter
λ μ€λ λ μμ ν ν΄λμ€μ΄λ€.
λν νΉμ ν¨ν΄μΌλ‘ ν¬λ§€ν°λ₯Ό λ§λ€ μ μλ μ μ ν©ν 리 λ©μλλ μ 곡νλ€.
1
2
3
4
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date1 = LocalDate.of(2014, 3, 18);
String formattedDate = date1.format(formatter);
LocalDate date2 = LocalDate.parse(formattedDate, formatter);
ofPattern
λ©μλλ Locale λ‘ ν¬λ§€ν°λ₯Ό λ§λ€ μ μλλ‘ μ€λ²λ‘λλ λ©μλλ₯Ό μ 곡νλ€.
1
2
3
DateTimeFormatter italianFormatter =
DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN);
...
DateTimeFormatterBuilder ν΄λμ€λ‘ 볡ν©μ μΈ ν¬λ§€ν°λ₯Ό μ μν΄μ μΈλΆμ μΌλ‘ μ μ΄ν μλ μλ€.
1
2
3
4
5
6
7
8
DateTimeFormatter italianFormatter = new DateTimeFormatterBuilder()
.appendText(ChronoField.DAY_OF_MONTH)
.appendLiteral(". ")
.appendText(ChronoField.MONTH_OF_YEAR)
.appendLiteral(" ")
.appendText(ChronoField.YEAR)
.parseCaseInsensitive()
.toFormatter(Locale.ITALIAN);
12.3 λ€μν μκ°λμ μΊλ¦°ν° νμ© λ°©λ²
κΈ°μ‘΄μ java.util.TimeZone
μ λ체ν μ μλ java.time.ZoneId
ν΄λμ€κ° μλ‘κ² λ±μ₯νλ€.
μ΄ ν΄λμ€λ₯Ό μ΄μ©νλ©΄ μ¨λ¨Ένμ(DST) κ°μ 볡μ‘ν μ¬νμ΄ μλμΌλ‘ μ²λ¦¬λλ€.
ZoneId ν΄λμ€λ λΆλ³μ΄λ€.
12.3.1 μκ°λ μ¬μ©νκΈ°
ZoneRules
ν΄λμ€μλ μ½ 40κ° μ λμ μκ°λκ° μκ³ , getRules() λ₯Ό μ΄μ©ν΄ ν΄λΉ μκ°λμ κ·μ μ νλν μ μλ€.
1
ZoneId romeZone = ZoneId.of("Europe/Rome");
μ§μ ID λ μ§μ/λμ
νμμΌλ‘ μ΄λ£¨μ΄μ§κ³ IANA Time Zone Datebase
μμ μ 곡νλ μ§μ μ§ν© μ 보λ₯Ό μ¬μ©νλ€.
ZoneId κ°μ²΄λ₯Ό μ»μ λ€μμλ LocalDate, LocalDateTime, Instant λ₯Ό μ΄μ©ν΄μ ZoneDateTime μΈμ€ν΄μ€λ‘ λ³νν μ μλ€.
1
2
3
4
5
6
LocalDate date = LocalDate.of(2014, Month.MARCH, 18);
ZonedDateTime zdt1 = date.atStartOfDay(romeZone);
LocalDateTime dateTime = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45);
ZonedDateTime zdt2 = dateTime.atZone(romeZone);
Instant instant = Instant.now();
ZonedDateTime zdt3 = instant.atZone(romeZone);
ZoneId λ₯Ό μ΄μ©ν΄μ LocalDateTime μ Instant λ‘ λ°κΏ μ μλ€.
1
2
Instant instant = Instant.now();
LocalDateTime timeFromInstant = LocalDateTime.ofInstant(instant, romeZone);
12.3.2 UTC/Greenwich κΈ°μ€μ κ³ μ μ€νμ
λλ‘λ νμ μΈκ³μ(UTC)/그리λμΉ νμ€μ(GMT) λ₯Ό κΈ°μ€μΌλ‘ νννκΈ°λ νλ€.
ZoneId μ μλΈν΄λμ€μΈ ZoneOffset ν΄λμ€λ‘ λ°λμ 그리λμΉ 0λ μμ€μ κ³Ό μκ°κ°μ μ°¨μ΄λ₯Ό ννν μ μλ€.
1
ZoneOffset newYorkOffset = ZoneOffset.of("-05:00");
μ΄λ μλ¨Ένμμ μ λλ‘ μ²λ¦¬ν μ μμ΄ κΆμ₯νμ§ μλλ€.
ISO-8601 μΊλ¦°λ μμ€ν μμ μ μνλ UTC/GMT μ μ€νμ μΌλ‘ λ μ§μ μκ°μ νννλ OffsetDateTime μ λ§λλ λ°©λ²λ μλ€.
1
2
LocalDateTime dateTime = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45);
OffsetDateTime dateTimeInNewYork = OffsetDateTime.of(date, newYorkOffset);
12.3.3 λμ μΊλ¦°λ μμ€ν μ¬μ©νκΈ°
μλ° 8μμλ μΆκ°λ‘ 4κ°μ μΊλ¦°λ μμ€ν μ μ 곡νλ€.
ThaiBuddhistDate, MinguoDate, JapaneseDate, HijrahDate μ΄λ€.
μ΄ ν΄λμ€μ LocalDate ν΄λμ€λ ChronoLocalDAte μΈν°νμ΄μ€λ₯Ό ꡬννλλ° μ΄λ μμμ μ°λκΈ°μμ νΉμ λ μ§λ₯Ό ννν μ μλ κΈ°λ₯μ μ 곡νλ μΈν°νμ΄μ€μ΄λ€.
1
2
LocalDate date = LocalDate.of(2014, Month.MARCH, 18);
JapaneseDate japaneseDate = JapaneseDate.from(date);
νΉμ Locale κ³Ό Locale μ λν λ μ§ μΈμ€ν΄μ€λ‘ μΊλ¦°λ μμ€ν μ λ§λ€μλ μλ€.
1
2
Chronology japaneseChronology = Chronology.ofLocale(Locale.JAPAN);
ChronoLocalDate now = japaneseChronology.dateNow();
λ μ§μ μκ° API μ μ€κ³μλ ChronoLocalDate 보λ€λ LocalDate λ₯Ό μ¬μ©νλΌκ³ κΆκ³ νλ€.