1. Date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sec06.exam06; | |
import java.util.*; | |
import java.text.*; | |
public class Hello { | |
public static void main(String[] args) { | |
Date now = new Date(); | |
System.out.println(now.toString()); | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 hh시 mm분 ss초"); | |
System.out.println(sdf.format(now)); | |
} | |
} |
결과:
Thu Jan 28 14:33:27 KST 2021
2021년 01월 28일 02시 33분 27초
2. Calendar
Calendar 클래스는 추상 클래스이므로 new 연산자를 통해 인스턴스를 생성할 수 없습니다. Calendar 클래스의 정적 메소드인 getInstance()를 이용하면 현재 운영체제에 설정되어 있는 시간대를 기준으로 한 Calendar 하위 객체를 얻을 수 있습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sec06.exam06; | |
import java.util.*; | |
import java.text.*; | |
public class Hello { | |
public static void main(String[] args) { | |
Calendar now = Calendar.getInstance(); | |
int year = now.get(Calendar.YEAR); | |
int month = now.get(Calendar.MONTH); | |
int day = now.get(Calendar.DAY_OF_MONTH); | |
int week = now.get(Calendar.DAY_OF_WEEK); | |
String strWeek = null; | |
switch(week) { | |
case Calendar.MONDAY: | |
strWeek = "월"; | |
break; | |
case Calendar.TUESDAY: | |
strWeek = "화"; | |
break; | |
case Calendar.WEDNESDAY: | |
strWeek = "수"; | |
break; | |
case Calendar.THURSDAY: | |
strWeek = "목"; | |
break; | |
case Calendar.FRIDAY: | |
strWeek = "금"; | |
break; | |
case Calendar.SATURDAY: | |
strWeek = "토"; | |
break; | |
case Calendar.SUNDAY: | |
strWeek = "일"; | |
break; | |
} | |
int amPm = now.get(Calendar.AM_PM); | |
String strAmPm = null; | |
if(amPm == Calendar.AM) { | |
strAmPm = "오전"; | |
} else { | |
strAmPm = "오후"; | |
} | |
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+1 + "월"); | |
System.out.println(day + "일"); | |
System.out.println(strWeek + "요일"); | |
System.out.println(strAmPm); | |
System.out.println(hour + "시"); | |
System.out.println(minute + ""); | |
System.out.println(second + "초"); | |
} | |
} |