- Calendar 클래스
Calendar 클래스
추상클래스임.
1. 추상메서드를 멤버로 가질수있다.
2. 객체화 불가. new Calendar() 오류.
=> 상속을 통해서 자손클래스의 객체화를 통해 객체화 가능.
3. abstract 예약어로 구현함.
getInstance() static 메서드를 통해서 객체 전달
📌
package chap12;
import java.util.Calendar;
public class CalendarEx1 {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println(now);
//get(상수값) : 지정된 상수값에 맞는 값을 리턴
System.out.println("년도:" + now.get(Calendar.YEAR));
System.out.println("월(0 ~ 11):" + (now.get(Calendar.MONTH) + 1));
System.out.println("일:" + now.get(Calendar.DATE));
System.out.println("일:" + now.get(Calendar.DAY_OF_MONTH));
System.out.println("년도기준일자:" + now.get(Calendar.DAY_OF_YEAR));
System.out.println("요일(1:일~7:토):" + now.get(Calendar.DAY_OF_WEEK));
System.out.println("월기준 몇째주:" + now.get(Calendar.WEEK_OF_MONTH));
System.out.println("년기준 몇째주:" + now.get(Calendar.WEEK_OF_YEAR));
System.out.println("월기준 몇째요일:" + now.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("오전0/오후1:" + now.get(Calendar.AM_PM));
System.out.println("시간(0~11):" + now.get(Calendar.HOUR));
System.out.println("시간(0~23):" + now.get(Calendar.HOUR_OF_DAY));
System.out.println("분(0~59):" + now.get(Calendar.MINUTE));
System.out.println("초(0~59):" + now.get(Calendar.SECOND));
System.out.println("밀리초(0~999):" + now.get(Calendar.MILLISECOND));
System.out.println("TimeZone(밀리초):" + now.get(Calendar.ZONE_OFFSET));
System.out.println("TimeZone(시간):" + (now.get(Calendar.ZONE_OFFSET))/(1000*60*60)); //시간대
System.out.println("이번달의 마지막일자:" + now.getActualMaximum(Calendar.DATE));
}
}
- Calander.getlnstance()
: 현재 일시
📌
package chap12;
import java.util.Calendar;
/*
* Calendar 객체로 날짜 설정하기
*/
public class CalendarEx2 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance(); //현재일시
//2022년 12월 31로 설정
cal.set(2022,(12-1),31); //날짜 설정. set(년도, 월(0~11), 일)
System.out.println("날짜:"+cal.get(Calendar.YEAR)+"년"+
(cal.get(Calendar.MONTH)+1)+"월"+
cal.get(Calendar.DATE)+ "일");
//cal 요일 출력하기
String week = "일월화수목금토일";
System.out.println(week.charAt(cal.get(Calendar.DAY_OF_WEEK)-1)+"요일");
}
}
📌
package chap12;
import java.util.Calendar;
import java.util.Date;
public class CalendarEx3 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
Date now = new Date();
now.setTime(now.getTime()+(1000*60*60*24));
cal.setTime(now);
System.out.println("날짜:"+cal.get(Calendar.YEAR)+"년"+
(cal.get(Calendar.MONTH)+1)+"월"+
cal.get(Calendar.DATE)+ "일");
Date day = new Date();
day.setTime(cal.getTimeInMillis());
System.out.println(day);
}
}
'수업(국비지원) > Java' 카테고리의 다른 글
| [Java] chap13 : 컬렉션 프레임워크 - Collection(List) (0) | 2023.04.17 |
|---|---|
| [Java] chap12: 기본 API - 기본 API Exam3. (0) | 2023.04.16 |
| [Java] chap12: 기본 API - 기본 API Exam2.(Date) (0) | 2023.04.16 |
| [Java] chap12: 기본 API - Date, getTime() (0) | 2023.04.16 |
| [Java] chap12: 기본 API - 기본 API Exam1 (0) | 2023.04.16 |