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.