Time API
Time API
λ μ§μ μκ°μ λ€λ£¨κΈ° μν APIλ‘ μλ° 8μμ λμ λμλ€.
μ£Όμ ν΄λμ€
-
LocalDate: λ μ§λ₯Ό νννλ ν΄λμ€ (μ: 2024-08-11)
-
LocalTime: μκ°(μ, λΆ, μ΄)μ νννλ ν΄λμ€ (μ: 14:30:00)
-
LocalDateTime: λ μ§μ μκ°μ λͺ¨λ νννλ ν΄λμ€ (μ: 2024-08-11T14:30:00)
-
ZonedDateTime: μκ°λ μ 보λ₯Ό ν¬ν¨νλ λ μ§μ μκ° ν΄λμ€
-
Duration: λ μμ κ°μ μκ° μ°¨μ΄λ₯Ό νννλ ν΄λμ€
-
Period: λ λ μ§ κ°μ μ°¨μ΄λ₯Ό μ°, μ, μΌ λ¨μλ‘ νννλ ν΄λμ€
-
DateTimeFormatter: λ μ§μ μκ°μ λ¬Έμμ΄λ‘ ν¬λ§·νκ±°λ λ¬Έμμ΄μ λ μ§μ μκ°μΌλ‘ νμ±νλ ν΄λμ€
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class TimeApiExample {
public static void main(String[] args) {
// νμ¬ λ μ§
LocalDate today = LocalDate.now();
System.out.println("μ€λ λ μ§: " + today);
// νμ¬ μκ°
LocalTime now = LocalTime.now();
System.out.println("νμ¬ μκ°: " + now);
// νμ¬ λ μ§μ μκ°
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("νμ¬ λ μ§μ μκ°: " + dateTime);
// λ μ§ μ°μ°
LocalDate today = LocalDate.now();
LocalDate futureDate = today.plus(Period.ofDays(10));
System.out.println("10μΌ ν λ μ§: " + futureDate);
// μκ° μ°μ°
LocalDateTime now = LocalDateTime.now();
LocalDateTime later = now.plus(Duration.ofHours(5));
System.out.println("5μκ° ν μκ°: " + later);
// λ μ§μ μκ° ν¬λ§·ν
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.format(formatter);
System.out.println("ν¬λ§·ν
λ λ μ§μ μκ°: " + formattedDateTime);
// λ¬Έμμ΄μ λ μ§μ μκ°μΌλ‘ νμ±
String dateTimeString = "2024-08-11 15:30:00";
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println("νμ±λ λ μ§μ μκ°: " + parsedDateTime);
}
}
LocalDate ν΄λμ€
λ μ§ μ 보λ₯Ό νννλ©°, μκ° μ 보λ ν¬ν¨λμ§ μλλ€.
-
now(): νμ¬ λ μ§ λ°ν
-
of(int year, int month, int dayOfMonth): νΉμ λ μ§ μμ±
-
plusDays(long daysToAdd): λ μ§ + μΌμ
-
minusMonths(long monthsToSubtract): λ μ§ - μμ
-
getDayOfWeek(): μμΌ λ°ν
-
parse(CharSequence text): λ¬Έμμ΄μ LocalDate λ‘ λ³ν
-
format(DateTimeFormatter formatter): LocalDateλ₯Ό μ§μ ν νμμΌλ‘ ν¬λ§·ν νμ¬ λ¬Έμμ΄λ‘ λ°ν
-
with(TemporalField field, long newValue): νΉμ νλμ κ°μ λ³κ²½ν μλ‘μ΄ LocalDate κ°μ²΄λ₯Ό λ°ν
-
isBefore(LocalDate otherDate) & isAfter(LocalDate otherDate): λ λ μ§λ₯Ό λΉκ΅νμ¬ νμ¬ λ μ§κ° λ€λ₯Έ λ μ§λ³΄λ€ μ΄μ λλ μ΄νμΈμ§ νμΈ
-
getDayOfYear(): ν΄λΉ μ°λμμ λͺ λ²μ§Έ λ μΈμ§ λ°ν
-
**lengthOfMonth() & lengthOfYear(): ν΄λΉ μ λλ μ°λμ μΌ μ λ°ν
1
2
3
4
5
6
7
8
9
10
11
LocalDate date = LocalDate.of(2024, 8, 11); // 2024λ
8μ 11μΌ
LocalDate tomorrow = date.plusDays(1); // 2024λ
8μ 12μΌ
LocalDate date = LocalDate.parse("2024-08-11");
String formattedDate = date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
LocalDate firstDayOfMonth = date.with(ChronoField.DAY_OF_MONTH, 1);
boolean isBefore = date.isBefore(LocalDate.now());
int dayOfYear = date.getDayOfYear();
int daysInMonth = date.lengthOfMonth();
LocalTime ν΄λμ€
μκ° μ 보λ₯Ό νννλ©°, λ μ§ μ 보λ ν¬ν¨λμ§ μλλ€.
-
now(): νμ¬ μκ° λ°ν
-
of(int hour, int minute, int second): νΉμ μκ° μμ±
-
plusHours(long hoursToAdd): μκ° + μκ°
-
minusMinutes(long minutesToSubtract): μκ° - λΆ
-
getHour(): μ λ°ν
-
toSecondOfDay() & toNanoOfDay(): ν루μ μμλΆν° κ²½κ³Όλ μ΄ λλ λλ Έμ΄ λ°ν
1
2
3
4
5
6
LocalTime time = LocalTime.of(15, 30); // 15μ 30λΆ
LocalTime later = time.plusMinutes(15); // 15μ 45λΆ
// λ μκ°μ λΉκ΅νμ¬ νμ¬ μκ°μ΄ λ€λ₯Έ μκ°λ³΄λ€ μ΄μ λλ μ΄νμΈμ§ νμΈν©λλ€
boolean isAfter = time.isAfter(LocalTime.NOON);
int secondsOfDay = time.toSecondOfDay();
LocalDateTime ν΄λμ€
λ μ§μ μκ° μ 보λ₯Ό λͺ¨λ νννλ€.
-
now(): νμ¬ λ μ§μ μκ° λ°ν
-
of(int year, int month, int dayOfMonth, int hour, int minute, int second): νΉμ λ μ§μ μκ° μμ±
-
plusDays(long daysToAdd): λ μ§ + μΌμ
-
minusHours(long hoursToSubtract): μκ° - μκ°μ
-
getDate(), getTime(): λ μ§μ μκ°μ λΆλ¦¬νμ¬ λ°ν
1
2
3
4
5
LocalDateTime dateTime = LocalDateTime.of(2024, 8, 11, 15, 30); // 2024λ
8μ 11μΌ 15μ 30λΆ
LocalDateTime futureDateTime = dateTime.plusHours(1); // 2024λ
8μ 11μΌ 16μ 30λΆ
LocalDate date = dateTime.toLocalDate();
LocalTime time = dateTime.toLocalTime();