糊言亂語(yǔ)

          志未半斤, 才無(wú)八兩. 有苦有樂(lè), 糊涂過(guò)活。
          posts - 25, comments - 7, trackbacks - 0, articles - 42
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

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

           

          DateFormatUtils – 提供格式化日期和時(shí)間的功能及相關(guān)常量;

          DateUtils – CalendarDate的基礎(chǔ)上提供更方便的訪問(wèn);

          DurationFormatUtils – 提供格式化時(shí)間跨度的功能及相關(guān)常量;

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

          StopWatch – 是一個(gè)方便的計(jì)時(shí)器。

           

          我們還是在一個(gè)例子中來(lái)看上述各個(gè)類的用法吧:

           

          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

                  }

              }

           

          }

           

          以下是運(yùn)行結(jié)果:

           

          ======= 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.

           

          具體的調(diào)用細(xì)節(jié)和完整的API請(qǐng)參閱Commons LangJavadoc
          主站蜘蛛池模板: 建水县| 芮城县| 甘德县| 彭水| 泰兴市| 夏邑县| 凤城市| 会同县| 福海县| 静宁县| 平凉市| 临澧县| 安溪县| 义乌市| 土默特左旗| 日照市| 肃南| 个旧市| 红安县| 丰台区| 伊川县| 榆中县| 濮阳县| 兴仁县| 文安县| 墨竹工卡县| 应用必备| 金溪县| 濮阳县| 那坡县| 宜良县| 石林| 浦东新区| 南雄市| 高陵县| 松滋市| 固原市| 昭通市| 湘乡市| 铜山县| 兰州市|