JAVA를 JAVA라

[JAVA] Calendar, 현재 날짜 출력해보기 본문

JAVA/풀어봅시다 (eclipse)

[JAVA] Calendar, 현재 날짜 출력해보기

샛별KIM 2021. 5. 31. 10:37
package may31;

import java.util.Calendar;

public class DateFormat02 {
	public static void main(String[] args) {
		//아래와 같이 출력하는 프로그램을 만들어주세요.
		// 2021년 5월 31일 월요일 오전 10시 30분 59초 입니다.
		
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = (cal.get(Calendar.MONTH)) + 1 ; //0부터 시작
		int date = cal.get(Calendar.DATE);
		int day = cal.get(Calendar.DAY_OF_WEEK);
		int iAmPm = cal.get(Calendar.AM_PM);
		int hour = cal.get(Calendar.HOUR);
		int min = cal.get(Calendar.MINUTE);
		int sec = cal.get(Calendar.SECOND);
		
		char dayOfWeek = 0;
		String ampm = null;
		
		switch (day) {
		case 0:
			dayOfWeek = '일';
			break;
		case 1:
			dayOfWeek = '월';
			break;
		case 2:
			dayOfWeek = '화';
			break;
		case 3:
			dayOfWeek = '수';
			break;
		case 4:
			dayOfWeek = '목';
			break;
		case 5:
			dayOfWeek = '금';
			break;
		case 6:
			dayOfWeek = '토';
			break;
		}
		
		ampm = iAmPm == 0 ? "오전" : "오후";
		
		System.out.printf("%d년 %d월 %d일 %c요일 %s %d시 %d분 %d초 입니다.", 
				year, month, day, dayOfWeek, ampm, hour, min, sec);
	}
}
Comments