Post

Date and Time

βœ… LocalDateTime

  • πŸ‘πŸ» get time of my local area

  • LocalDate: 2023-11-21
  • LocalTime: 00:20:30.234
  • LocalDateTime: LocalDate + LocalTime, 2023-11-21T00:20:30.234

  • now()
1
2
3
4
5
6
7
LocalDate now = LocalDate.now(); //2025-05-01
LocalTime now = LocalTime.now(); //17:55:06.305827

LocalDateTime now = LocalDateTime.now(); //2025-05-01T17:57:05.637786
LocalDate date = now.toLocalDate(); //get only date from LocalDateTime
LocalTime time = now.toLocalTime(); //get only time from from LocalDateTime
LocalDateTime addDateTime = LocalDateTime.of(date, time); //λ‚ μ§œ, μ‹œκ°„ ν•©μΉ˜κΈ°
  • isBefore()
  • isAfter()
  • isEquals()

βœ… ZonedDateTime

  • UTC, summertime에 λŒ€ν•œ 정보 ν¬ν•¨ν•œ μ‹œκ°„
  • for developing international time
  • πŸ‘πŸ» apply both UTC, summertime
1
2
ZoneId.getAvailableZoneIds() // λ‚΄κ°€ μ‹œκ°„ 얻을 수 μžˆλŠ” μ§€μ—­ μ–»κΈ°
ZoneId zoneId = ZoneId.systemDefault(); //λ‚΄ μ‹œμŠ€ν…œμ΄ μ–΄λ”” μ§€μ—­ μ‹œκ°„ μ“°λŠ”μ§€
  • βœ”οΈ Get other country times
1
2
3
4
ZonedDateTime seoul = ZonedDateTime.of(LocalDate.of(2024, 1, 1), LocalTime.of(9, 0, 0), ZoneId.of("Asia/Seoul"));
ZonedDateTime london = seoul.withZoneSameInstant(ZoneId.of("Europe/London"));
ZonedDateTime newyork = seoul.withZoneSameInstant(ZoneId.of("America/New_York"));

βœ… OffsetDateTime

  • UTC에 λŒ€ν•œ 정보 ν¬ν•¨ν•œ μ‹œκ°„
  • πŸ‘πŸ» apply UTC, do not apply summertime

βœ… Instant

  • how much time passed since 1970-01-01 00:00:00
  • show how much seconds passed
  • πŸ‘πŸ» used when the time has to same all over the world
1
2
Instant now = Instant.now();
Instant epochSecond = Instant.ofEpochSecond(0);

βœ… Period and Duration

  • amount of time

  • βœ”οΈ Period
  • amount of time in year, month, days
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Period period = Period.ofDays(10); //10days


LocalDate localDate = LocalDate.of(2020, 3, 3);
LocalDate plus = localDate.plus(period); //3 + 10 = 13일


//κΈ°κ°„μ˜ 차이 κ΅¬ν•˜κΈ°
LocalDate startDate = LocalDate.of(2020, 3, 3);
LocalDate endDate = LocalDate.of(2025, 5, 1);

Period between = Period.between(startDate, endDate);
between.getYears(); //5λ…„
between.getMonths(); //1κ°œμ›”
between.getDays(); //28일
  • βœ”οΈ Duration
  • amount of time in hour, minutes, seconds

βœ… Datetime Interface

  • LocalDateTime, ZonedDateTime, OffsetDateTime extends interface Temporal and TemporalAccessor
  • βœ”οΈ interface TemporalAccessor: read date, time
  • βœ”οΈ interface Temporal: modify date, time

  • Period and Duration extends interface TemporalAmount
  • βœ”οΈ interface TemporalAmount: amount of time

  • ChronoUnit extends interface TemporalUnit
  • βœ”οΈ interface TemporalUnit: μ‹œκ°„ λ‹¨μœ„ 제곡
  • minutes, day, years, decades…

  • ChronoField extends interface TemporalField
  • βœ”οΈ interface TemporalField: λ‚ μ§œμ™€ μ‹œκ°„μ˜ νŠΉμ • 뢀뢄을 λ‚˜νƒ€λƒ„
    • 예λ₯Ό λ“€μ–΄ 8μ›” 16일일 λ•Œ, MONTH_OF_YEAR: 8, DAY_OF_YEAR: 16
1
2
ChronoField.MONTH_OF_YEAR.range(); //1-12
ChronoField.DAY_OF_YEAR.range() //1-365/366

βœ… Get and modify Date, time

  • βœ”οΈ Get date, time
1
2
3
4
LocalDateTime dt = LocalDateTime.of(2020, 1, 2, 3, 40, 50);

dt.get(ChronoField.YEAR); //2020
dt.getYear(); //2020
  • βœ”οΈ Add time
  • same result, in three ways
1
2
3
4
5
6
7
8
9
10
LocalDateTime dt = LocalDateTime.of(2020, 1, 2, 3, 40, 50);

// 1️⃣ ChronoUnit
LocalDateTime years1 = dt.plus(10, ChronoUnit.YEARS); //2030
// 2️⃣ plusYears(simplified method of ChronoUnit)
LocalDateTime years2 = dt.plusYears(10); //2030

3️⃣ Period μ‚¬μš©
Period period = Period.ofYears(10);
LocalDateTime years3 = dt.plus(period); //2030
  • βœ”οΈ Check if I can get this value from this class
1
2
LocalDate now = LocalDate.now();
now.isSupported(ChronoField.SECOND_OF_DAY); //can I get SECOND_OF_DAY from LocalDate
  • βœ”οΈ Modify time
  • with()
1
2
3
4
5
6
7
8
9
10
11
LocalDateTime dt = LocalDateTime.of(2020, 1, 2, 3, 40, 50);
LocalDateTime changedDt1 = dt.with(ChronoField.YEAR, 2030); //change year to 2030

LocalDateTime changedDt2 = dt.withYear(2030);

//temporal adjuster
//change to next week
LocalDateTime nextFriday = dt.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
//last sunday of this month
LocalDateTime sameMonthLastSunday = dt.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));

βœ… Date ➑️ String

  • βœ”οΈ Date ➑️ String
1
2
3
4
LocalDateTime dateTime = LocalDateTime.of(2020, 1, 2, 3, 40, 59);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyλ…„ MMμ›” dd일 HH:mm:ss");
String formattedDateTime = dateTime.format(formatter); //2020λ…„ 01μ›” 02일 03:40:59
  • βœ”οΈ String ➑️ Date
1
2
String input = "2020λ…„ 09μ›” 22일 12:34:32";
LocalDateTime parsedDateTime = LocalDateTime.parse(input, formatter); //2020-09-22T12:34:32

βœ…

βœ…

This post is licensed under CC BY 4.0 by the author.