Date, Calendar
Java에서 시간을 사용하는 2가지 방법에 대해서 정리하였습니다.
Date, Calendar인데 Date에 대응되는 메소드는 추천하지 않는다고 API 도움말에 나와있습니다. 그리고 대부분은 Date 메소드는 Deprecated. 가 붙어있습니다.
https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
The corresponding methods in Date are deprecated.
예제
package testProject; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Test { public static void main(String[] args) { System.out.println("1 ======================"); Date curdate1 = new Date(); System.out.println("Date:"+curdate1); Calendar curcal1 = Calendar.getInstance(); System.out.println("Calendar:"+curcal1); System.out.println("2 ======================"); // Calendar to Date 1 Date curdate2 = curcal1.getTime(); System.out.println("Calendar to Date M1:"+curdate2); // Calendar to Date 2 Date curdate3 = new Date(curcal1.getTimeInMillis()); System.out.println("Calendar to Date M2:"+curdate3); // Date to Calendar Calendar caldatetest = Calendar.getInstance(); caldatetest.set(2000, 1/*Month:0-Jan, 1-Feb...*/, 1); System.out.println("set Calendar 2000/1/1:"+caldatetest.getTime()); caldatetest.setTime(curdate2); System.out.println("Date to Calendar:"+caldatetest.getTime()); System.out.println("3 ======================"); // Date toString() System.out.println("Date.toString():"+curdate3.toString()); // Date.setTime curdate2.setTime(0); System.out.println("Date.seTime(0):"+curdate2); curdate2.setTime(1000); System.out.println("Date.seTime(1000):"+curdate2); System.out.println("4 ======================"); // Print date Format SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("SimpleDateFormat.format:"+format.format(curdate1)); // Load data format Date savedDate = new Date(0); try { savedDate = format.parse("2017-04-17 00:17:29"); } catch (java.text.ParseException e) { e.printStackTrace(); } System.out.println("SimpleDateFormat.parse:"+format.format(savedDate)); System.out.println("5 ======================"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar afterDate = Calendar.getInstance(); Calendar beforeDate = Calendar.getInstance(); Calendar baseDate = Calendar.getInstance(); afterDate.set(2000, 2, 3, 4, 5); baseDate.set(2000, 1, 2, 3, 4); beforeDate.set(2000, 0, 1, 2, 3); System.out.println("afterDate:"+dateFormat.format(afterDate.getTime())); System.out.println("baseDate:"+dateFormat.format(baseDate.getTime())); System.out.println("beforeDate:"+dateFormat.format(beforeDate.getTime())); if(baseDate.before(beforeDate)){ System.out.println("before true:"+dateFormat.format(beforeDate.getTime())); } if(baseDate.before(afterDate)){ System.out.println("before true:"+dateFormat.format(afterDate.getTime())); } if(baseDate.after(beforeDate)){ System.out.println("after true:"+dateFormat.format(beforeDate.getTime())); } if(baseDate.after(afterDate)){ System.out.println("after true:"+dateFormat.format(afterDate.getTime())); } } }
결과
1 ====================== Date:Sun Apr 30 15:18:06 KST 2017 Calendar:java.util.GregorianCalendar[time=1493533086063,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Seoul",offset=32400000,dstSavings=0,useDaylight=false,transitions=22,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=3,WEEK_OF_YEAR=18,WEEK_OF_MONTH=6,DAY_OF_MONTH=30,DAY_OF_YEAR=120,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=18,SECOND=6,MILLISECOND=63,ZONE_OFFSET=32400000,DST_OFFSET=0] 2 ====================== Calendar to Date M1:Sun Apr 30 15:18:06 KST 2017 Calendar to Date M2:Sun Apr 30 15:18:06 KST 2017 set Calendar 2000/1/1:Tue Feb 01 15:18:06 KST 2000 Date to Calendar:Sun Apr 30 15:18:06 KST 2017 3 ====================== Date.toString():Sun Apr 30 15:18:06 KST 2017 Date.seTime(0):Thu Jan 01 09:00:00 KST 1970 Date.seTime(1000):Thu Jan 01 09:00:01 KST 1970 4 ====================== SimpleDateFormat.format:2017-04-30 15:18:06 SimpleDateFormat.parse:2017-04-17 00:17:29 5 ====================== afterDate:2000-03-03 04:05:06 baseDate:2000-02-02 03:04:06 beforeDate:2000-01-01 02:03:06 before true:2000-03-03 04:05:06 after true:2000-01-01 02:03:06
예제 설명
초기화
Date, Calendar 를 초기화 하는 방법 입니다. 두개 모두 기본값으로 현재 시각을 가져옵니다.System.out.println("1 ======================"); Date curdate1 = new Date(); System.out.println("Date:"+curdate1); Calendar curcal1 = Calendar.getInstance(); System.out.println("Calendar:"+curcal1);
결과
1 ====================== Date:Sun Apr 30 14:38:26 KST 2017 Calendar:java.util.GregorianCalendar[time=1493530706461,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Seoul",offset=32400000,dstSavings=0,useDaylight=false,transitions=22,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=3,WEEK_OF_YEAR=18,WEEK_OF_MONTH=6,DAY_OF_MONTH=30,DAY_OF_YEAR=120,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=38,SECOND=26,MILLISECOND=461,ZONE_OFFSET=32400000,DST_OFFSET=0]
변환
Calendar를 Date로 변환하는 방법입니다. getTime(), getTimeMillis() 메소드를 이용하면 됩니다. 반대로 Date를 Calendar로 변환 하는 방법은 setTime()메소드를 사용하면 됩니다.Calendar.set시 월의 경우 0이 1월입니다.
- A year y is represented by the integer y
- 1900
. - A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
- A date (day of month) is represented by an integer from 1 to 31 in the usual manner.
- An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12.
- A minute is represented by an integer from 0 to 59 in the usual manner.
- A second is represented by an integer from 0 to 61; the values 60 and 61 occur only for leap seconds and even then only in Java implementations that actually track leap seconds correctly. Because of the manner in which leap seconds are currently introduced, it is extremely unlikely that two leap seconds will occur in the same minute, but this specification follows the date and time conventions for ISO C.
System.out.println("2 ======================"); // Calendar to Date 1 Date curdate2 = curcal1.getTime(); System.out.println("Calendar to Date M1:"+curdate2); // Calendar to Date 2 Date curdate3 = new Date(curcal1.getTimeInMillis()); System.out.println("Calendar to Date M2:"+curdate3); // Date to Calendar Calendar caldatetest = Calendar.getInstance(); caldatetest.set(2000, 1/*Month:0-Jan, 1-Feb...*/, 1); System.out.println("set Calendar 2000/1/1:"+caldatetest.getTime()); caldatetest.setTime(curdate2); System.out.println("Date to Calendar:"+caldatetest.getTime());
결과
2 ====================== Calendar to Date M1:Sun Apr 30 14:38:26 KST 2017 Calendar to Date M2:Sun Apr 30 14:38:26 KST 2017 set Calendar 2000/1/1:Tue Feb 01 14:38:26 KST 2000 Date to Calendar:Sun Apr 30 14:38:26 KST 2017
date의 시간 설정
setTime()에 메소드에 시간 정보를 적게되는데 결과를 보면 1000 차이가 1초를 나타냅니다. 즉 ms단위로 기록을 해줘야 하며 0 의값은 1970/1/1 의 값을 가짐을 알 수 있습니다. 9시인 이유는 시스템 설정이 GMT+9라서 그렇습니다.System.out.println("3 ======================"); // Date toString() System.out.println("Date.toString():"+curdate3.toString()); // Date.setTime curdate2.setTime(0); System.out.println("Date.seTime(0):"+curdate2); curdate2.setTime(1000); System.out.println("Date.seTime(1000):"+curdate2);
결과
3 ====================== Date.toString():Sun Apr 30 14:38:26 KST 2017 Date.seTime(0):Thu Jan 01 09:00:00 KST 1970 Date.seTime(1000):Thu Jan 01 09:00:01 KST 1970
날짜 format
시간을 표현하는데에는 SimpleDateFormat 을 이용하면 원하는 포맷으로 출력하거나 입력 받을 수 있습니다.System.out.println("4 ======================"); // Print date Format SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("SimpleDateFormat.format:"+format.format(curdate1)); // Load data format Date savedDate = new Date(0); try { savedDate = format.parse("2017-04-17 00:17:29"); } catch (java.text.ParseException e) { e.printStackTrace(); } System.out.println("SimpleDateFormat.parse:"+format.format(savedDate));
결과
4 ====================== SimpleDateFormat.format:2017-04-30 14:38:26 SimpleDateFormat.parse:2017-04-17 00:17:29
시간 비교
시간 비교 메소드는 before(), after()를 사용하게 됩니다. 자기 자신이 기준이 되므로 현재시각.before()는 자기 시각이 인자로 주어지는 시각에 비해 앞에 있으면 true가 됩니다.
System.out.println("5 ======================"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar afterDate = Calendar.getInstance(); Calendar beforeDate = Calendar.getInstance(); Calendar baseDate = Calendar.getInstance(); afterDate.set(2000, 2, 3, 4, 5); baseDate.set(2000, 1, 2, 3, 4); beforeDate.set(2000, 0, 1, 2, 3); System.out.println("afterDate:"+dateFormat.format(afterDate.getTime())); System.out.println("baseDate:"+dateFormat.format(baseDate.getTime())); System.out.println("beforeDate:"+dateFormat.format(beforeDate.getTime())); if(baseDate.before(beforeDate)){ System.out.println("before true:"+dateFormat.format(beforeDate.getTime())); } if(baseDate.before(afterDate)){ System.out.println("before true:"+dateFormat.format(afterDate.getTime())); } if(baseDate.after(beforeDate)){ System.out.println("after true:"+dateFormat.format(beforeDate.getTime())); } if(baseDate.after(afterDate)){ System.out.println("after true:"+dateFormat.format(afterDate.getTime())); }
결과
5 ====================== afterDate:2000-03-03 04:05:06 baseDate:2000-02-02 03:04:06 beforeDate:2000-01-01 02:03:06 before true:2000-03-03 04:05:06 after true:2000-01-01 02:03:06
댓글 없음:
댓글 쓰기