목차

날짜 관련

  • description : 자바에서 날짜 관련 자료
  • author : 주레피
  • email : dhan@repia.com
  • lastupdate : 2020-04-01

Date Format 유형

yyyy.MM.dd HH:mm:ss.S 

Class SimpleDateFormat

Calendar

1. 사용법

Calendar cal = Calendar.getInstance();
 
int year = cal.get(Calendar.YEAR);
int mon = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);
int sec = cal.get(Calendar.SECOND);
 
System.out.println("현재시간");
System.out.println(year + "년 "+ mon+"월 "+day+"일");
System.out.println(hour + "시 "+ min+"분 "+sec+"초");   


2. 주의하자!! 2월 or 30일

Calendar cal = Calendar.getInstance();    // 현재 시간을 받음.
 
cal.set(Calendar.YEAR , 1994);
cal.set(Calendar.MONTH , Calendar.JUNE);
cal.set(Calendar.DAY_OF_MONTH , 28);
 
cal.set(Calendar.HOUR_OF_DAY , 14);
cal.set(Calendar.MINUTE , 25);
cal.set(Calendar.SECOND , 19);
 
System.out.println("Year : " + cal.get(Calendar.YEAR));
System.out.println("Month : " + (cal.get(Calendar.MONTH)+1) ); //괄호 안쪽에 연산을 해줌
System.out.println("Day of Month : " + cal.get(Calendar.DAY_OF_MONTH));        
System.out.println("Sun(1) ~ Sat(7) : " + cal.get(Calendar.DAY_OF_WEEK));            
System.out.println("Hour(0 ~ 23): " + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("Minute(0 ~ 59): " + cal.get(Calendar.MINUTE));
System.out.println("Second(0 ~ 59): " + cal.get(Calendar.SECOND));
 
System.out.println( cal.get(Calendar.YEAR) + "년 " + (cal.get(Calendar.MONTH)+1)
                       + "월 " + cal.get(Calendar.DAY_OF_MONTH) + "일");
 
System.out.println( cal.get(Calendar.HOUR_OF_DAY) + "시 " + cal.get(Calendar.MINUTE)
                       + "분 " + cal.get(Calendar.SECOND) + "초");
2월 30일은 3월로 표기, 4월 31일은 5월로 표기됨


Ref