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.