본문 바로가기

콩's EDUCATION/콩's JAVA_RUN

SimpleDateFormat

SimpleDateFormat 예제


 

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarTest {
 public static void main(String[] args) {
  Calendar now = Calendar.getInstance();
  // 년도-월-일-시-분-초
  int year = now.get(Calendar.YEAR);
  // 5월값 저장변수 january = 0, May = 4 이다.
  int month = now.get(Calendar.MONTH)+1;
  int day = now.get(Calendar.DAY_OF_MONTH);
  int hour = now.get(Calendar.HOUR_OF_DAY);
  int min = now.get(Calendar.MINUTE);
  int sec = now.get(Calendar.SECOND);  
    
  System.out.println(year+"년 "+month+"월 "+day+"일 "
    +hour+"시 "+min+"분 "+sec+"초 ");
  
  now.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR)+100);
  
  int year2 = now.get(Calendar.YEAR);
  // 5월값 저장변수 january = 0, May = 4 이다.
  int month2 = now.get(Calendar.MONTH)+1;
  int day2 = now.get(Calendar.DAY_OF_MONTH);
  int hour2 = now.get(Calendar.HOUR_OF_DAY);
  int min2 = now.get(Calendar.MINUTE);
  int sec2 = now.get(Calendar.SECOND);
  
  System.out.println(year2+"년 "+month2+"월 "+day2+"일 "
    +hour2+"시 "+min2+"분 "+sec2+"초 ");
  
  // 14/05/20 ; 2014-5-20 변경 'YYYY-MM-DD'
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMM/dd/HH/mm/ss/SSS/EEE");
  System.out.println(sdf.format(now.getTime()));
  
  /**
   *   System.out.println(now);
   * 모든 정보가 아래와 같이 표시된다. 시간은 1/1000초
   * java.util.GregorianCalendar
   * [time=1400565918062,areFieldsSet=true,
   * areAllFieldsSet=true,lenient=true,
   * zone=sun.util.calendar.ZoneInfo[id="Asia/Seoul",
   * offset=32400000,dstSavings=0,useDaylight=false,
   * transitions=14,lastRule=null],firstDayOfWeek=1,
   * minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=4,
   * WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=20,
   * DAY_OF_YEAR=140,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=3,
   * AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=5,SECOND=18,
   * MILLISECOND=62,ZONE_OFFSET=32400000,DST_OFFSET=0]
   */

 }

}

 


 

CalendarTest.java