LocalDate
LocalDate
λ μ§λ§ μ¬μ©ν μ¬μ©
LocalDate today = LocalDate.now(); // νμ¬ λ μ§ μΆλ ₯ (νμ¬ λ μ§)
LocalDate date = LocalDate.of(νμ¬μ°λ, νμ¬μ, νμ¬μΌ); // νΉμ λ μ§ μΆλ ₯ (νμ¬ λ μ§)
date.getYear(); // μ°λ : νμ¬μ°λ
date.getMonth(); // μ : νμ¬μ
date.getMonthValue(); // μ : νμ¬μ
date.getDateOfMonth(); // μΌ : νμ¬μΌ
date.getDayOfYear(); // μ¬ν΄ λ©°μΉ μ§Έ : νμ¬ λ μ§μ μ°μ€ μΌ μ
date.getDayOfWeek(); // μμΌ : νμ¬ μμΌ
date.isLeapYear(); // μ΄λ
μ¬λΆ : νμ¬ μ°λκ° μ€λ
μΈμ§ μ¬λΆ
// LocalDate -> String
date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
// String -> LocalDate
LocalDate.parse("νμμ λ§λ λ¬Έμμ΄");
// LocalDate -> LocalDateTime
date.atTime(νμ¬μ, νμ¬λΆ);
LocalTime
μκ°λ§ μ¬μ©ν μ¬μ©
LocalTime now = LocalTime.now(); // νμ¬ μκ° μΆλ ₯ (νμ¬ μκ°)
LocalTime time = LocalTime.of(νμ¬μ, νμ¬λΆ, νμ¬μ΄, νμ¬λλ
Έμ΄); // νΉμ μκ° μΆλ ₯ (νμ¬ μκ°)
time.getHour(); // μ : νμ¬μ
time.getMinute(); // λΆ : νμ¬λΆ
time.getSecond(); // μ΄ : νμ¬μ΄
time.getNano(); // λλ
Έμ΄ : νμ¬λλ
Έμ΄
LocalDateTime
λ μ§μ μκ° λͺ¨λ μ¬μ©ν μ¬μ©
LocalDateTime now = LocalDateTime.now(); // νμ¬ λ μ§μ μκ° μΆλ ₯ (νμ¬ λ μ§ λ° μκ°)
LocalDateTime time = LocalDateTime.of(νμ¬μ°λ, νμ¬μ, νμ¬μΌ, νμ¬μ, νμ¬λΆ); // νΉμ λ μ§μ μκ° μΆλ ₯ (νμ¬ λ μ§ λ° μκ°)
// LocalDate + LocalTime μ‘°ν©
LocalDateTime time2 = LocalDateTime.of(LocalDate.now(), LocalTime.now());
// LocalDateTime -> String
time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// String -> LocalDateTime
LocalDateTime.parse("νμμ λ§λ λ¬Έμμ΄");
LocalDateTime.parse("νμμ λ§λ λ¬Έμμ΄", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// LocalDateTime -> LocalDate
LocalDate.from(time);
λ μ§ μ°¨μ΄ κ³μ°
Period ν΄λμ€
LocalDate now = LocalDate.now();
LocalDate target = LocalDate.of(νμ¬μ°λ, νμ¬μ, νμ¬μΌ);
Period period = Period.between(now, target);
period.getYears(); // νμ¬μ νκ² λ μ§ κ°μ μ° μ
period.getMonths(); // νμ¬μ νκ² λ μ§ κ°μ μ μ
period.getDays(); // νμ¬μ νκ² λ μ§ κ°μ μΌ μ
μκ° κΈ°μ€μΌλ‘ κ³μ°νκΈ° (ChronoUnit)
LocalDate now = LocalDate.now();
LocalDate target = LocalDate.of(νμ¬μ°λ, νμ¬μ, νμ¬μΌ);
ChronoUnit.DAYS.between(now, target); // νμ¬μ νκ² λ μ§ κ°μ μΌ μ
μκ° μ°¨μ΄ κ³μ°
Duration ν΄λμ€
LocalTime now = LocalTime.now(); // νμ¬ μκ° μΆλ ₯ (νμ¬ μκ°)
LocalTime target = LocalTime.of(νμ¬μ, νμ¬λΆ, νμ¬μ΄); // νΉμ μκ° μΆλ ₯ (νμ¬ μκ°)
Duration duration = Duration.between(now, target);
duration.getSeconds(); // νμ¬μ νκ² μκ° κ°μ μ΄ μ
duration.getNano(); // νμ¬μ νκ² μκ° κ°μ λλ
Έμ΄ μ
YearMonth
λ κ³Ό μλ§ μ¬μ©ν κ²½μ° μ¬μ©ν μ μλ ν΄λμ€ (yyyy-MM)
YearMonth date = YearMonth.of(νμ¬μ°λ, νμ¬μ); // νμ¬ μ°λμ μ μΆλ ₯ (νμ¬ μ°λμ μ)
date.getYear(); // νμ¬ μ°λ
date.getMonth(); // νμ¬ μ
date.getMonthValue()); // νμ¬ μ
date.atEndOfMonth(); // νμ¬ μμ λ§μ§λ§ λ μ§
date.lengthOfMonth(); // νμ¬ μμ μΌ μ
date.plusMonths(1); // νμ¬ μμμ ν λ¬ ν
date.minusMonths(1); // νμ¬ μμμ ν λ¬ μ
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.Duration;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.YearMonth;
public class Main {
public static void main(String[] args) {
// νμ¬ λ μ§ λ° μκ°
LocalDate today = LocalDate.now();
LocalTime now = LocalTime.now();
LocalDateTime currentDateTime = LocalDateTime.now();
// νΉμ λ μ§ λ° μκ°
LocalDate date = LocalDate.of(2024, 5, 7);
LocalTime time = LocalTime.of(14, 30);
LocalDateTime dateTime = LocalDateTime.of(2024, 5, 7, 14, 30);
// LocalDate λ©μλ
int year = today.getYear(); // μ°λ : 2024
int month = today.getMonthValue(); // μ : 5
int dayOfMonth = today.getDayOfMonth(); // μΌ : 7
int dayOfYear = today.getDayOfYear(); // μ¬ν΄ λ©°μΉ μ§Έ : 128
LocalDate tomorrow = today.plusDays(1); // λ΄μΌ λ μ§
// LocalTime λ©μλ
int hour = now.getHour(); // μ : 14
int minute = now.getMinute(); // λΆ : 30
int second = now.getSecond(); // μ΄ : 15
// LocalDateTime λ©μλ
LocalDateTime nextMonth = currentDateTime.plusMonths(1); // ν λ¬ ν
// λ μ§ νμ μ§μ
String formattedDate = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 2024-05-07
String formattedDateTime = currentDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); // 2024-05-07 14:30:15
// λ μ§ μ°¨μ΄ κ³μ°
Period period = Period.between(today, date); // νμ¬ λ μ§μ date κ°μ κΈ°κ°
long daysBetween = ChronoUnit.DAYS.between(today, date); // νμ¬ λ μ§μ date κ°μ μΌ μ
// μκ° μ°¨μ΄ κ³μ°
Duration duration = Duration.between(now, time); // νμ¬ μκ°κ³Ό time κ°μ κΈ°κ°
}
}
8λ²μ μμλ μλ‘μ΄ λ μ§μ μκ°μ λ€λ£¨λ ν΄λμ€μΈ LocalDate, LocalTime, LocalDateTimeμ΄ λμ
λμμ΅λλ€(java.time).
μ΄μ λ μ΄λ¬ν ν΄λμ€λ₯Ό μ¬μ©νμ¬ λ μ§μ μκ°μ λ€λ£¨λ κ²μ΄ κΆμ₯λ©λλ€.
μ΄μ μ μ¬μ©λλ Dateμ Calendar ν΄λμ€λ μ¬μ©μ μ§μν΄μΌ ν©λλ€(java.util).
μ°Έκ³ λΈλ‘κ·Έ: europani.github.io
Leave a comment