본문 바로가기
  • Coding & Book
학습단/JAVA 학습단

14. 혼공자 14일차(java.util 패키지)

by 루이3 2023. 7. 20.

Date 클래스

  • 날짜를 표현하는 클래스 입니다.
  • 날짜 정보를 주고 받을때 매개 변수나 리턴 타입으로 주로 사용됩니다.
package sec12.exam01;

import java.text.*;
import java.util.*;

public class DateExample {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Date now = new Date();
		String strNow1 = now.toString();		
		System.out.println(strNow1);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 hh시 mm분 ss초");
		String Now2 = sdf.format(now);
		System.out.println(Now2);
	}

}

 

 

Calendar 클래스

  • 달력을 표현한 클래스 입니다.
  • 추상 클래스이미로 new 연산자를 사용해서 인스턴스를 생성할수 없습니다.
package sec12.exam01;
import java.util.*;

public class CalendarExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Calendar now = Calendar.getInstance();
		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);
		System.out.println(year);
		System.out.println(month);
		System.out.println(day);
		System.out.println(week);
		System.out.println(amPm);
		System.out.println(hour);
		System.out.println(minute);
		System.out.println(second);
		
	}

}

 

'학습단 > JAVA 학습단' 카테고리의 다른 글

15. 혼공자 15일차(멀티 스레드)  (0) 2023.07.24
3주차 완료  (0) 2023.07.20
13. 혼공자 13일차(기본 API 클래스)  (0) 2023.07.19
12. 혼공자 12일차(예외 처리)  (0) 2023.07.18
11. 혼공자 11일차(예외)  (0) 2023.07.17