Date, Calendar 클래스는 java.util 패키지에 포함되어 있다.
1. Date 클래스
날짜를 표현하는 클래스로 객체 간에 날짜 정보를 주고 받을 때 주로 사용된다.
Date 클래스에는 여러 개의 생성자가 선언되어 있지만, 대부분 비권장(Deprecated) 되어
Date() 생성자만 주로 사용된다.
컴퓨터의 현재 날짜를 읽어 Date 객체로 만든다.
Date now = new Date();
현재 날짜를 문자열로 얻고 싶다면 toString() 메소드를 사용하면 된다.
영문으로 된 날짜를 리턴하고, 만약 특정 문자열 포멧으로 얻고 싶다면,
java.text.SimpleDateFormat 클래스를 이용한다.
예) DateExample.java : 현재 날짜 출력
public class DateExample {
public static void main(String[] args) {
Date now = new Date();
String strNow1 = now.toString();
System.out.println(strNow1);
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy년 MM월 dd시 mm분 ss초”);
String strNow2 = sdf.format(now);
System.out.println(strNow2);
}
}
2. Calendar 클래스
달력을 표현한 클래스이다. 추상 클래스이므로 new 연산자를 사용해서 인스턴스를 생성할 수 없다.
(날짜, 시간 계산이 지역, 나라 마다 다르기 때문이다.)
Calendar 클래스의 정적 메소드 getInstance() 메소드를 이용하면,
현재 운영체제에 설정되어 있는 시간대(TimeZone)를 기준으로 한 Calendar 하위 객체를 얻을 수 있다.
Calendar now = Calendar getInstance();
Calendar 객체를 얻었다면 get 메소드를 통해 날짜와 시간에 대한 정보를 읽을 수 있다.
int year = now.get(Calendar.YEAR); // 년도 리턴
int month = now.get(Calendar.MONTH) + 1; // 월 리턴
int day = now.get(Calendar.DAY_OF_MONTH); // 일 리턴
int week = now.get(Calendar.DAY_OF_WEEK); // 요일 리턴
int ampm = now.get(Calendar.AM_PM); // 오전 / 오후 리턴
int hour = now.get(Calendar.HOUR); // 시간 리턴
int minute = now.get(Calendar.MINUTE); // 분 리턴
int second = now.get(Calendar.SECOND); // 초 리턴
다른 시간대로 바꾸는 방법은 Calendar 클래스의 오버로딩된 다른 getInstance() 메소드를 이용하면,
다른 시간대의 calendar 를 얻을 수 있다.
알고 싶은 시간대의 java.util.TimeZone 객체를 얻어서,
Calendar.getInstance() 메소드의 매개값으로 넘겨주면 된다.
TimeZone timeZone = TimeZone.getTimeZone(“America/Los_Angeles”);
Calendar now = Calendar.getInstance(timeZone);
예) PrintTimeZoneID.java : 사용 가능한 시간대 문자열 출력
public class PrintTimeZoneID {
public static void main(String[] args) {
String[] availableIDs = TimeZone.getAvailableIDs();
for(String id : availableIDs) {
System.out.println(id);
}
}
}
'Java 기본 문법 - 참조 서적 [이것이 자바다 - 한빛미디어] > 9. 기본 API 클래스' 카테고리의 다른 글
16. Java 자바 [API] - java.time 패키지 (0) | 2020.10.08 |
---|---|
15. Java 자바 [API] - Format 클래스 (0) | 2020.09.29 |
13. Java 자바 [API] - Math, Random 클래스 (0) | 2020.09.29 |
12. Java 자바 [API] - Wrapper (포장) 클래스 (0) | 2020.09.29 |
11. Java 자바 [API] - Arrays 클래스 (0) | 2020.09.29 |
Date, Calendar 클래스는 java.util 패키지에 포함되어 있다.
1. Date 클래스
날짜를 표현하는 클래스로 객체 간에 날짜 정보를 주고 받을 때 주로 사용된다.
Date 클래스에는 여러 개의 생성자가 선언되어 있지만, 대부분 비권장(Deprecated) 되어
Date() 생성자만 주로 사용된다.
컴퓨터의 현재 날짜를 읽어 Date 객체로 만든다.
Date now = new Date();
현재 날짜를 문자열로 얻고 싶다면 toString() 메소드를 사용하면 된다.
영문으로 된 날짜를 리턴하고, 만약 특정 문자열 포멧으로 얻고 싶다면,
java.text.SimpleDateFormat 클래스를 이용한다.
예) DateExample.java : 현재 날짜 출력
public class DateExample {
public static void main(String[] args) {
Date now = new Date();
String strNow1 = now.toString();
System.out.println(strNow1);
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy년 MM월 dd시 mm분 ss초”);
String strNow2 = sdf.format(now);
System.out.println(strNow2);
}
}
2. Calendar 클래스
달력을 표현한 클래스이다. 추상 클래스이므로 new 연산자를 사용해서 인스턴스를 생성할 수 없다.
(날짜, 시간 계산이 지역, 나라 마다 다르기 때문이다.)
Calendar 클래스의 정적 메소드 getInstance() 메소드를 이용하면,
현재 운영체제에 설정되어 있는 시간대(TimeZone)를 기준으로 한 Calendar 하위 객체를 얻을 수 있다.
Calendar now = Calendar getInstance();
Calendar 객체를 얻었다면 get 메소드를 통해 날짜와 시간에 대한 정보를 읽을 수 있다.
int year = now.get(Calendar.YEAR); // 년도 리턴
int month = now.get(Calendar.MONTH) + 1; // 월 리턴
int day = now.get(Calendar.DAY_OF_MONTH); // 일 리턴
int week = now.get(Calendar.DAY_OF_WEEK); // 요일 리턴
int ampm = now.get(Calendar.AM_PM); // 오전 / 오후 리턴
int hour = now.get(Calendar.HOUR); // 시간 리턴
int minute = now.get(Calendar.MINUTE); // 분 리턴
int second = now.get(Calendar.SECOND); // 초 리턴
다른 시간대로 바꾸는 방법은 Calendar 클래스의 오버로딩된 다른 getInstance() 메소드를 이용하면,
다른 시간대의 calendar 를 얻을 수 있다.
알고 싶은 시간대의 java.util.TimeZone 객체를 얻어서,
Calendar.getInstance() 메소드의 매개값으로 넘겨주면 된다.
TimeZone timeZone = TimeZone.getTimeZone(“America/Los_Angeles”);
Calendar now = Calendar.getInstance(timeZone);
예) PrintTimeZoneID.java : 사용 가능한 시간대 문자열 출력
public class PrintTimeZoneID {
public static void main(String[] args) {
String[] availableIDs = TimeZone.getAvailableIDs();
for(String id : availableIDs) {
System.out.println(id);
}
}
}
'Java 기본 문법 - 참조 서적 [이것이 자바다 - 한빛미디어] > 9. 기본 API 클래스' 카테고리의 다른 글
16. Java 자바 [API] - java.time 패키지 (0) | 2020.10.08 |
---|---|
15. Java 자바 [API] - Format 클래스 (0) | 2020.09.29 |
13. Java 자바 [API] - Math, Random 클래스 (0) | 2020.09.29 |
12. Java 자바 [API] - Wrapper (포장) 클래스 (0) | 2020.09.29 |
11. Java 자바 [API] - Arrays 클래스 (0) | 2020.09.29 |