org.apache.commons.lang.time

 

org.apache.commons.lang.time에 대한 연구

  •  DateFormatUtils
  •  DateUtils
  •  FastDateFormat
  •  DurationFormatUtils
  •  StopWatch

icon DateUtils(CommonsTimeUtilsDemo.java)

 소스코드(litwave-utils-demo.zip)

  1.  

    package litwave.utils.demo;

  2.  

  3. import java.text.ParseException;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.Locale;
    import java.util.TimeZone;

  4.  

  5. import org.apache.commons.lang.time.DateFormatUtils;
    import org.apache.commons.lang.time.DateUtils;
    import org.apache.commons.lang.time.DurationFormatUtils;
    import org.apache.commons.lang.time.FastDateFormat;
    import org.apache.commons.lang.time.StopWatch;

  6.  

  7. public class CommonsTimeUtilsDemo {
        /**
         * DateUtils, DateFormatUtils 클래스를 이용하는 Sample 프로그램

         *   샘플설명 혹은 샘플 제작에 있어서 어떤 클래스에 대한 Sample 이란 형태보다 어떠한 작업을 하는
         *      샘플인데 이러한 클래스를 이용한 무슨 무슨 프로그램처럼~ 해주는게 더 어떨까함 -dan-
         * @throws ParseException
         */
        public void dateUtilsDemo() throws ParseException {
            String [] parsePatterns  = new String [] {
                    "yyyyMMddHHmmss", "yyyy'-'MM'-'dd HH':'mm':'ss", "yyyy'-'MM'-'dd',' HH':'mm':'ss"
                };
            String [] yyyymmddhhmiss = new String [] {
                    "2009-02-10 16:22:32", "2009-02-10 13:20:05"
                };
            Date date1, date2;
            int sequence = 1;
            System.out.println("---------------------------------------------------------------------");
            System.out.println("[ DateUtils, DateFormatUtils ]");
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.parseDate, DateFormatUtils.format");
            System.out.println("-> Input          : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            System.out.println("-> Output(date)   : " + DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns));
            System.out.println("-> Output(format) : " + DateFormatUtils.format(date1, "yyyy-MM-dd, HH:mm:ss"));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateFormatUtils.format(millis, \"yyyy-MM-dd, HH:mm:ss\")");
            long millis = System.currentTimeMillis();
            System.out.println("-> Input          : " + millis);
            System.out.println("-> Output(format) : " + DateFormatUtils.format(millis, "yyyy-MM-dd, HH:mm:ss"));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.addDays(date, 3)");
            System.out.println("-> Input          : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            System.out.println("-> Output(date)   : " + DateUtils.addDays(date1, 3));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.addMonths(date, 2)");
            System.out.println("-> Input          : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            System.out.println("-> Output(date)   : " + DateUtils.addMonths(date1, 2));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.addWeeks(date, 2)");
            System.out.println("-> Input          : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            System.out.println("-> Output(date)   : " + DateUtils.addWeeks(date1, 2));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.isSameDay(date1, date2))");
            System.out.println("-> Input 1        : " + yyyymmddhhmiss[0]);
            System.out.println("-> Input 2        : " + yyyymmddhhmiss[1]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            date2 = DateUtils.parseDate(yyyymmddhhmiss[1], parsePatterns);
            System.out.println("-> Output         : " + DateUtils.isSameDay(date1, date2));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.iterator(date1, DateUtils.RANGE_WEEK_CENTER)");
            System.out.println("-> Input 1        : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            Iterator iter = DateUtils.iterator(date1, DateUtils.RANGE_WEEK_CENTER);
            while (iter.hasNext()) {
                System.out.println("-> Output         : " + ((Calendar)iter.next()).getTime());
            }
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.iterator(date1, DateUtils.RANGE_WEEK_CENTER)");
            System.out.println("-> Input 1        : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            iter = DateUtils.iterator(date1, DateUtils.RANGE_WEEK_RELATIVE);
            while (iter.hasNext()) {
                System.out.println("-> Output         : " + ((Calendar)iter.next()).getTime());
            }
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.round");
            System.out.println("-> Input                : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            System.out.println("-> Output(DAY_OF_MONTH) : " + DateUtils.round(date1, Calendar.DAY_OF_MONTH));
            System.out.println("-> Output(HOUR_OF_DAY)  : " + DateUtils.round(date1, Calendar.HOUR_OF_DAY));
            System.out.println("-> Output(MINUTE)       : " + DateUtils.round(date1, Calendar.MINUTE));
            System.out.println("-> Output(SECOND)       : " + DateUtils.round(date1, Calendar.SECOND));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.truncate");
            System.out.println("-> Input                : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            System.out.println("-> Output(DAY_OF_MONTH) : " + DateUtils.truncate(date1, Calendar.DAY_OF_MONTH));
            System.out.println("-> Output(HOUR_OF_DAY)  : " + DateUtils.truncate(date1, Calendar.HOUR_OF_DAY));
            System.out.println("-> Output(MINUTE)       : " + DateUtils.truncate(date1, Calendar.MINUTE));
            System.out.println("-> Output(SECOND)       : " + DateUtils.truncate(date1, Calendar.SECOND));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DateUtils.setDays");
            System.out.println("-> Input                           : " + yyyymmddhhmiss[0]);
            date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            System.out.println("-> Output(setMonths(date1, 5 - 1)) : " + DateUtils.setMonths(date1, 5 - 1));
            System.out.println("-> Output(setDays(date1, 7))       : " + DateUtils.setDays(date1, 7));
            System.out.println("-> Output(setHours(date1, 2))      : " + DateUtils.setHours(date1, 2));
            System.out.println("-> Output(setMinutes(date1, 9))    : " + DateUtils.setMinutes(date1, 9));
            System.out.println("-> Output(setMilliseconds(date1, 213)) : " + DateUtils.setMilliseconds(date1, 213));
            System.out.println("---------------------------------------------------------------------");
        }

  8.     /**
         * FastDateFormat 클래스를 이용하는 Sample 프로그램
         * @throws ParseException
         */
        public void fastDateFormatDemo() throws ParseException {
            String parrern = "yyyy-MM-dd, HH:mm:ss";
            String [] parsePatterns  = new String [] {
                    "yyyyMMddHHmmss", "yyyy'-'MM'-'dd HH':'mm':'ss", "yyyy'-'MM'-'dd',' HH':'mm':'ss"
                };
            String [] yyyymmddhhmiss = new String [] {
                    "2009-02-10 16:22:32", "2009-02-10 13:20:05"
                };
            Date date1  = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            Date date2  = DateUtils.parseDate(yyyymmddhhmiss[1], parsePatterns);
            long millis = date2.getTime();
            FastDateFormat fastDateFormat
                = FastDateFormat.getInstance(parrern, TimeZone.getTimeZone("GMT+09:00"), Locale.KOREA);

  9.         int sequence = 1;
            System.out.println("---------------------------------------------------------------------");
            System.out.println("[ FastDateFormat ]");
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". FastDateFormat");
            System.out.println("---------------------------------------------------------------------");
            System.out.println(">>> FastDateFormat fastDateFormat ");
            System.out.println("         = FastDateFormat.getInstance(parrern, ");
            System.out.println("               TimeZone.getTimeZone(\"GMT+09:00\"), Locale.KOREA);");
            System.out.println("---------------------------------------------------------------------");
            System.out.println("-> Input[" + yyyymmddhhmiss[0] + "]  : " + date1);
            System.out.println("-> Input[" + yyyymmddhhmiss[1] + "]  : " + millis);
            System.out.println("-> fastDateFormat              : " + fastDateFormat);
            System.out.println("-> Output[" + yyyymmddhhmiss[0] + "] : " + fastDateFormat.format(date1));
            System.out.println("-> Output[" + yyyymmddhhmiss[1] + "] : " + fastDateFormat.format(millis));
            System.out.println("---------------------------------------------------------------------");
        }

  10.     /**
         * DurationFormatUtils 클래스를 이용하는 Sample 프로그램
         * @throws ParseException
         */
        public void durationFormatUtilsDemo() throws ParseException {
            String [] parsePatterns  = new String [] {
                    "yyyyMMddHHmmss", "yyyy'-'MM'-'dd HH':'mm':'ss", "yyyy'-'MM'-'dd',' HH':'mm':'ss"
                };
            String [] yyyymmddhhmiss = new String [] {
                    "2009-02-10 16:22:32", "2009-02-08 13:20:05", "2007-05-12 13:20:05"
                };
            Date date1 = DateUtils.parseDate(yyyymmddhhmiss[0], parsePatterns);
            Date date2 = DateUtils.parseDate(yyyymmddhhmiss[1], parsePatterns);
            Date date3 = DateUtils.parseDate(yyyymmddhhmiss[2], parsePatterns);
            int sequence = 1;

  11.         System.out.println("---------------------------------------------------------------------");
            System.out.println("[ DurationFormatUtils ]");
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DurationFormatUtils.formatDuration");
            System.out.println("---------------------------------------------------------------------");
            long periodMillis = date1.getTime() - date2.getTime();
            System.out.println("-> Input : [" + yyyymmddhhmiss[0] + "] - [" + yyyymmddhhmiss[1] + "] = " + periodMillis);
            System.out.println("-> Output[formatDurationHMS(periodMillis)] : "
                    + DurationFormatUtils.formatDurationHMS(periodMillis));
            System.out.println("-> Output[formatDuration(periodMillis, \"ddd일, HH:mm:ss.S\")]"
                    + "\n                : " + DurationFormatUtils.formatDuration(periodMillis, "ddd일, HH:mm:ss.S"));
            System.out.println("---------------------------------------------------------------------");
            System.out.println("DEMO " + sequence++ + ". DurationFormatUtils.formatPeriod");
            System.out.println("---------------------------------------------------------------------");
            System.out.println("-> Input[" + yyyymmddhhmiss[2] + "] : " + date3.getTime());
            System.out.println("-> Input[" + yyyymmddhhmiss[0] + "] : " + date1.getTime());
            System.out.println("-> Output[formatPeriodISO] : "
                    + DurationFormatUtils.formatPeriodISO(date3.getTime(), date1.getTime()));
            System.out.println("-> Output[formatPeriod] : "
                    + DurationFormatUtils.formatPeriod(date3.getTime(), date1.getTime(), "yy-MM-dd, HH:mm:ss"));
            System.out.println("---------------------------------------------------------------------");
        }

  12.     /**
         * StopWatch 클래스를 이용하는 Sample 프로그램
         * @throws InterruptedException
         */
        public void stopWatchDemo() throws InterruptedException {
            String parrern = "yyyy-MM-dd, HH:mm:ss.SSS";
            FastDateFormat fastDateFormat
                = FastDateFormat.getInstance(parrern, TimeZone.getTimeZone("GMT+09:00"), Locale.KOREA);
            StopWatch stopWatch = new StopWatch();

  13.         System.out.println("---------------------------------------------------------------------");
            System.out.println("[ StopWatch ]");
            System.out.println("---------------------------------------------------------------------");
            stopWatch.start();
            System.out.println("[Process]      : stopWatch.start() -> [" + stopWatch.getTime() + "]");
            Thread.sleep(100L);
            System.out.println("[Process]      : Thread.sleep(100L) -> [" + stopWatch.getTime() + "]");
            Thread.sleep(2000L);
            System.out.println("[Process]      : Thread.sleep(2000L) -> [" + stopWatch.getTime() + "]");
            Thread.sleep(200L);
            System.out.println("[Process]      : Thread.sleep(200L) -> [" + stopWatch.getTime() + "]");
            stopWatch.stop();
            System.out.println("[Process]      : stopWatch.stop() -> [" + stopWatch.getTime() + "]");
            Thread.sleep(1000L);
            System.out.println("[Process]      : Thread.sleep(1000L) -> [" + stopWatch.getTime() + "]");
            System.out.println("[START-TIME]   : "
                    + fastDateFormat.format(stopWatch.getStartTime()));
            System.out.println("[END-TIME]     : "
                    + fastDateFormat.format(stopWatch.getStartTime() + stopWatch.getTime()));
            System.out.println("[TIME]         : "
                    + stopWatch.getTime());
            System.out.println("[Execute-Time] : "
                    + stopWatch);
            System.out.println("---------------------------------------------------------------------");
        }

  14.  

  15.     public static void main(String [] args) throws Exception {
            CommonsTimeUtilsDemo demo = new CommonsTimeUtilsDemo();
            demo.dateUtilsDemo();
            demo.fastDateFormatDemo();
            demo.durationFormatUtilsDemo();
            demo.stopWatchDemo();
        }
    }


 

 

  출력 결과 : dateUtilsDemo()

  1.  
  2. -------------------------------------------------------------------------------
    [ DateUtils, DateFormatUtils ]
    -------------------------------------------------------------------------------
    DEMO 1. DateUtils.parseDate, DateFormatUtils.format
    -> Input          : 2009-02-10 16:22:32
    -> Output(date)   : Tue Feb 10 16:22:32 KST 2009
    -> Output(format) : 2009-02-10, 16:22:32
    -------------------------------------------------------------------------------
    DEMO 2. DateFormatUtils.format(millis, "yyyy-MM-dd, HH:mm:ss")
    -> Input          : 1234330122484
    -> Output(format) : 2009-02-11, 14:28:42
    -------------------------------------------------------------------------------
    DEMO 3. DateUtils.addDays(date, 3)
    -> Input          : 2009-02-10 16:22:32
    -> Output(date)   : Fri Feb 13 16:22:32 KST 2009
    -------------------------------------------------------------------------------
    DEMO 4. DateUtils.addMonths(date, 2)
    -> Input          : 2009-02-10 16:22:32
    -> Output(date)   : Fri Apr 10 16:22:32 KST 2009
    -------------------------------------------------------------------------------
    DEMO 5. DateUtils.addWeeks(date, 2)
    -> Input          : 2009-02-10 16:22:32
    -> Output(date)   : Tue Feb 24 16:22:32 KST 2009
    -------------------------------------------------------------------------------
    DEMO 6. DateUtils.isSameDay(date1, date2))
    -> Input 1        : 2009-02-10 16:22:32
    -> Input 2        : 2009-02-10 13:20:05
    -> Output         : true
    -------------------------------------------------------------------------------
    DEMO 7. DateUtils.iterator(date1, DateUtils.RANGE_WEEK_CENTER)
    -> Input 1        : 2009-02-10 16:22:32
    -> Output         : Sat Feb 07 00:00:00 KST 2009
    -> Output         : Sun Feb 08 00:00:00 KST 2009
    -> Output         : Mon Feb 09 00:00:00 KST 2009
    -> Output         : Tue Feb 10 00:00:00 KST 2009
    -> Output         : Wed Feb 11 00:00:00 KST 2009
    -> Output         : Thu Feb 12 00:00:00 KST 2009
    -> Output         : Fri Feb 13 00:00:00 KST 2009
    -------------------------------------------------------------------------------
    DEMO 8. DateUtils.iterator(date1, DateUtils.RANGE_WEEK_CENTER)
    -> Input 1        : 2009-02-10 16:22:32
    -> Output         : Tue Feb 10 00:00:00 KST 2009
    -> Output         : Wed Feb 11 00:00:00 KST 2009
    -> Output         : Thu Feb 12 00:00:00 KST 2009
    -> Output         : Fri Feb 13 00:00:00 KST 2009
    -> Output         : Sat Feb 14 00:00:00 KST 2009
    -> Output         : Sun Feb 15 00:00:00 KST 2009
    -> Output         : Mon Feb 16 00:00:00 KST 2009
    -------------------------------------------------------------------------------
    DEMO 9. DateUtils.round
    -> Input                : 2009-02-10 16:22:32
    -> Output(DAY_OF_MONTH) : Wed Feb 11 00:00:00 KST 2009
    -> Output(HOUR_OF_DAY)  : Tue Feb 10 16:00:00 KST 2009
    -> Output(MINUTE)       : Tue Feb 10 16:23:00 KST 2009
    -> Output(SECOND)       : Tue Feb 10 16:22:32 KST 2009
    -------------------------------------------------------------------------------
    DEMO 10. DateUtils.truncate
    -> Input                : 2009-02-10 16:22:32
    -> Output(DAY_OF_MONTH) : Tue Feb 10 00:00:00 KST 2009
    -> Output(HOUR_OF_DAY)  : Tue Feb 10 16:00:00 KST 2009
    -> Output(MINUTE)       : Tue Feb 10 16:22:00 KST 2009
    -> Output(SECOND)       : Tue Feb 10 16:22:32 KST 2009
    -------------------------------------------------------------------------------
    DEMO 11. DateUtils.setDays
    -> Input                           : 2009-02-10 16:22:32
    -> Output(setMonths(date1, 5 - 1)) : Sun May 10 16:22:32 KST 2009
    -> Output(setDays(date1, 7))       : Sat Feb 07 16:22:32 KST 2009
    -> Output(setHours(date1, 2))      : Tue Feb 10 02:22:32 KST 2009
    -> Output(setMinutes(date1, 9))    : Tue Feb 10 16:09:32 KST 2009
    -> Output(setMilliseconds(date1, 213)) : Tue Feb 10 16:22:32 KST 2009
    ===============================================================================

 

  출력 결과 : fastDateFormatDemo()

  1.  

  2. -------------------------------------------------------------------------------
    [ FastDateFormat ]
    -------------------------------------------------------------------------------
    DEMO 1. FastDateFormat
    -------------------------------------------------------------------------------
    >>> FastDateFormat fastDateFormat
             = FastDateFormat.getInstance(parrern,
                   TimeZone.getTimeZone("GMT+09:00"), Locale.KOREA);
    -------------------------------------------------------------------------------
    -> Input[2009-02-10 16:22:32]  : Tue Feb 10 16:22:32 KST 2009
    -> Input[2009-02-10 13:20:05]  : 1234239605000
    -> fastDateFormat              : FastDateFormat[yyyy-MM-dd, HH:mm:ss]
    -> Output[2009-02-10 16:22:32] : 2009-02-10, 16:22:32
    -> Output[2009-02-10 13:20:05] : 2009-02-10, 13:20:05
    ===============================================================================

 

 

  출력 결과 : durationFormatUtilsDemo()

  1.  

  2. -------------------------------------------------------------------------------
    [ DurationFormatUtils ]
    -------------------------------------------------------------------------------
    DEMO 1. DurationFormatUtils.formatDuration
    -------------------------------------------------------------------------------
    -> Input : [2009-02-10 16:22:32] - [2009-02-08 13:20:05] = 183747000
    -> Output[formatDurationHMS(periodMillis)] : 51:02:27.000
    -> Output[formatDuration(periodMillis, "ddd일, HH:mm:ss.S")]
                    : 002일, 03:02:27.000
    -------------------------------------------------------------------------------
    DEMO 2. DurationFormatUtils.formatPeriod
    -------------------------------------------------------------------------------
    -> Input[2007-05-12 13:20:05] : 1178943605000
    -> Input[2009-02-10 16:22:32] : 1234250552000
    -> Output[formatPeriodISO] : P1Y8M29DT3H2M27.000S
    -> Output[formatPeriod] : 01-08-29, 03:02:27
    ===============================================================================

 

 

  출력 결과 : stopWatchDemo()

  1.  

  2. -------------------------------------------------------------------------------
    [ StopWatch ]
    -------------------------------------------------------------------------------
    [Process]      : stopWatch.start() -> [0]
    [Process]      : Thread.sleep(100L) -> [94]
    [Process]      : Thread.sleep(2000L) -> [2094]
    [Process]      : Thread.sleep(200L) -> [2297]
    [Process]      : stopWatch.stop() -> [2297]
    [Process]      : Thread.sleep(1000L) -> [2297]
    [START-TIME]   : 2009-02-11, 14:28:42.531
    [END-TIME]     : 2009-02-11, 14:28:44.828
    [TIME]         : 2297
    [Execute-Time] : 0:00:02.297
    ===============================================================================

 

 

 

제가 말씀드리고 싶었던 것은 요런 것임 +_+~

조금 더 부연하면 UTIL 클래스에 format 의 생성스트링이 아닌 format 클래스를 필요한 것을 정리해두는 형태가 더 좋지 않을까라는

생각이 듭니당. +_+ 그냥 패턴의 출력형태로 format() 메소드만을 쓰기 위한거라면 구지 FastDateFormat 쓸필요 없이 DateForatUtils 의 format()을

쓰는게 맞는 것 같구요 +_+~ 

  1.   long currTime = System.currentTimeMillis();

  2.   System.out.println(DateFormatUtils.ISO_DATE_FORMAT.format(currTime));
      System.out.println(DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(currTime));
      System.out.println(DateFormatUtils.ISO_DATETIME_FORMAT.format(currTime));
      System.out.println(DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(currTime));
      System.out.println(DateFormatUtils.ISO_TIME_FORMAT.format(currTime));
      System.out.println(DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(currTime));
      System.out.println(DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(currTime));
      System.out.println(DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(currTime));
      System.out.println(DateFormatUtils.SMTP_DATETIME_FORMAT.format(currTime));

 

위의 소스를 실행한 결과네요. 결과 표현방식은 ISO8601 의 여러 포맷들과 제일 하단은 SMTP 에서 사용하는 DATETIME 포맷입니다. 

2009-02-11
2009-02-11+09:00
2009-02-11T10:08:17
2009-02-11T10:08:17+09:00
T10:08:17
10:08:17
T10:08:17+09:00
10:08:17+09:00
Wed, 11 Feb 2009 10:08:17 +0900