JAVA/포스팅

자바 날짜와 시간

짜집퍼박사(짜박) 2023. 11. 16. 01:24

Java에서 날짜와 시간을 다루기 위한 주요 클래스들은 java.util 및 java.time 패키지에 포함되어 있습니다. Java 8 이전에는 java.util.Date 및 java.util.Calendar가 주로 사용되었으나, Java 8부터는 java.time 패키지에서 새로운 API가 소개되었습니다.

 

1. java.util.Date (Java 7 이전)

Date 클래스는 날짜와 시간을 나타내는 클래스로, 다만 Java 8 이전에는 많은 문제가 있어서 현재는 권장되지 않습니다.

import java.util.Date;

public class DateExample {
    public static void main(String[] args) {
        // 현재 날짜와 시간 얻기
        Date currentDate = new Date();
        System.out.println(currentDate);

        // 특정 날짜로 설정
        Date specificDate = new Date(121, 10, 1); // 2021년 11월 1일 (년도는 1900을 빼서 사용)
        System.out.println(specificDate);
    }
}

 

2. java.util.Calendar (Java 7 이전)

Calendar 클래스는 날짜와 시간을 다루기 위한 추상 클래스입니다. GregorianCalendar 등의 하위 클래스를 통해 구체적인 구현을 얻을 수 있습니다.

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarExample {
    public static void main(String[] args) {
        // 현재 날짜와 시간 얻기
        Calendar currentCalendar = Calendar.getInstance();
        System.out.println(currentCalendar.getTime());

        // 특정 날짜로 설정
        Calendar specificCalendar = new GregorianCalendar(2021, Calendar.NOVEMBER, 1);
        System.out.println(specificCalendar.getTime());
    }
}

 

3. java.time 패키지 (Java 8 이후)

Java 8부터는 java.time 패키지에서 새로운 API가 제공됩니다. 이 API는 이전에 사용된 Date 및 Calendar 클래스의 한계를 극복하고, 강력하면서도 사용하기 쉬운 메서드들을 제공합니다.

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;

public class DateTimeExample {
    public static void main(String[] args) {
        // 현재 날짜 얻기
        LocalDate currentDate = LocalDate.now();
        System.out.println(currentDate);

        // 현재 시간 얻기
        LocalTime currentTime = LocalTime.now();
        System.out.println(currentTime);

        // 현재 날짜와 시간 얻기
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println(currentDateTime);

        // 특정 날짜와 시간 설정
        LocalDate specificDate = LocalDate.of(2021, 11, 1);
        LocalTime specificTime = LocalTime.of(12, 30, 0);
        LocalDateTime specificDateTime = LocalDateTime.of(specificDate, specificTime);
        System.out.println(specificDateTime);
    }
}

ZonedDateTime

import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        // 현재 날짜와 시간 얻기 (특정 타임존)
        ZonedDateTime currentZonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println(currentZonedDateTime);

        // 특정 날짜와 시간, 타임존 설정
        ZonedDateTime specificZonedDateTime = ZonedDateTime.of(
            2021, 11, 1, 12, 30, 0, 0, ZoneId.of("America/Los_Angeles")
        );
        System.out.println(specificZonedDateTime);
    }
}

이러한 클래스들은 각각 특정한 사용 사례에 적합하게 설계되어 있으며, 필요에 따라 선택하여 사용할 수 있습니다. Java 8 이상을 사용하는 경우에는 java.time 패키지를 사용하는 것이 권장됩니다.

 

With ChatGPT

'JAVA > 포스팅' 카테고리의 다른 글

자바 Date  (0) 2023.11.16
자바 Calendar  (0) 2023.11.16
자바 java.math.BigDecimal클래스  (0) 2023.11.16
자바 java.math.BigInteger클래스  (0) 2023.11.16
자바 java.util.StringTokenizer클래스  (0) 2023.11.16