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
extendsinterface Temporal
andTemporalAccessor
- โ๏ธ
interface TemporalAccessor
: read date, time โ๏ธ
interface Temporal
: modify date, timePeriod
andDuration
extendsinterface TemporalAmount
โ๏ธ
interface TemporalAmount
: amount of timeChronoUnit
extendsinterface TemporalUnit
- โ๏ธ
interface TemporalUnit
: ์๊ฐ ๋จ์ ์ ๊ณต minutes, day, years, decadesโฆ
ChronoField
extendsinterface TemporalField
- โ๏ธ
interface TemporalField
: ๋ ์ง์ ์๊ฐ์ ํน์ ๋ถ๋ถ์ ๋ํ๋- ์๋ฅผ ๋ค์ด 8์ 16์ผ์ผ ๋,
MONTH_OF_YEAR: 8
,DAY_OF_YEAR: 16
- ์๋ฅผ ๋ค์ด 8์ 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.