Vincent.Chan‘s Blog

          常用鏈接

          統計

          積分與排名

          網站

          最新評論

          [Jakarta Commons筆記] org.apache.commons.lang.time

          好了,來看我在Common Lang中最后要講的一個包:org.apache.commons.lang.time。這個包里面包含了如下5個類:

           

          DateFormatUtils – 提供格式化日期和時間的功能及相關常量;

          DateUtils – CalendarDate的基礎上提供更方便的訪問;

          DurationFormatUtils – 提供格式化時間跨度的功能及相關常量;

          FastDateFormat – java.text.SimpleDateFormat提供一個的線程安全的替代類;

          StopWatch – 是一個方便的計時器。

           

          我們還是在一個例子中來看上述各個類的用法吧:

           

          package sean.study.jakarta.commons.lang;

           

          import java.util.Calendar;

          import java.util.Date;

           

          import org.apache.commons.lang.StringUtils;

          import org.apache.commons.lang.time.DateFormatUtils;

          import org.apache.commons.lang.time.DateUtils;

          import org.apache.commons.lang.time.FastDateFormat;

          import org.apache.commons.lang.time.StopWatch;

           

          public class DateTimeUsage {

           

              public static void main(String[] args) {

                  demoDateUtils();

                  demoStopWatch();

              }

             

              public static void demoDateUtils() {

                  System.out.println(StringUtils.center(" demoDateUtils ", 30, "="));

                  Date date = new Date();

                  String isoDateTime = DateFormatUtils.ISO_DATETIME_FORMAT.format(date);

                  String isoTime = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(date);

                  FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM");

                  String customDateTime = fdf.format(date);

                  System.out.println("ISO_DATETIME_FORMAT: " + isoDateTime);

                  System.out.println("ISO_TIME_NO_T_FORMAT: " + isoTime);

                  System.out.println("Custom FastDateFormat: " + customDateTime);

                  System.out.println("Default format: " + date);

                  System.out.println("Round HOUR: " + DateUtils.round(date, Calendar.HOUR));

                  System.out.println("Truncate HOUR: " + DateUtils.truncate(date, Calendar.HOUR));

                  System.out.println();

              }

             

              public static void demoStopWatch() {

                  System.out.println(StringUtils.center(" demoStopWatch ", 30, "="));

                  StopWatch sw = new StopWatch();

                  sw.start();

                  operationA();

                  sw.stop();

                  System.out.println("operationA used " + sw.getTime() + " milliseconds.");

                  System.out.println();

              }

             

              public static void operationA() {

                  try {

                      Thread.sleep(999);

                  }

                  catch (InterruptedException e) {

                      // do nothing

                  }

              }

           

          }

           

          以下是運行結果:

           

          ======= demoDateUtils ========

          ISO_DATETIME_FORMAT: 2005-08-01T12:41:51

          ISO_TIME_NO_T_FORMAT: 12:41:51

          Custom FastDateFormat: 2005-08

          Default format: Mon Aug 01 12:41:51 CST 2005

          Round HOUR: Mon Aug 01 13:00:00 CST 2005

          Truncate HOUR: Mon Aug 01 12:00:00 CST 2005

           

          ======= demoStopWatch ========

          operationA used 1000 milliseconds.

           

          具體的調用細節和完整的API請參閱Commons LangJavadoc。

          posted on 2006-03-10 23:08 Vincent.Chen 閱讀(202) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 南皮县| 新安县| 呼伦贝尔市| 庆城县| 军事| 石城县| 峨边| 临泉县| 仁寿县| 庆城县| 河源市| 日喀则市| 五原县| 庆安县| 昌乐县| 聊城市| 资兴市| 应城市| 红安县| 土默特右旗| 黔江区| 恩施市| 漠河县| 盘锦市| 屏东县| 新丰县| 晋州市| 宕昌县| 丰城市| 葵青区| 邹城市| 林口县| 九寨沟县| 崇明县| 神农架林区| 寿光市| 化州市| 丰原市| 木里| 巴林右旗| 湖南省|