Post

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();
This post is licensed under CC BY 4.0 by the author.