TNT blog  
          日歷
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456
          統計
          • 隨筆 - 5
          • 文章 - 40
          • 評論 - 7
          • 引用 - 0

          導航

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          home

          搜索

          •  

          最新隨筆

          最新評論

          閱讀排行榜

           

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

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

          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.

          posted on 2006-06-01 11:12 TNT 閱讀(493) 評論(0)  編輯  收藏 所屬分類: apache
           
          Copyright © TNT Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 泰州市| 永吉县| 龙州县| 玛沁县| 巴青县| 中方县| 满洲里市| 观塘区| 永嘉县| 海原县| 财经| 陆河县| 常宁市| 江油市| 元阳县| 乌兰县| 青川县| 同德县| 化州市| 新乡市| 西丰县| 勃利县| 礼泉县| 唐河县| 松江区| 绥江县| 南阳市| 吉木萨尔县| 舟曲县| 文登市| 西安市| 广德县| 玉溪市| 阿鲁科尔沁旗| 永寿县| 卓尼县| 黑龙江省| 营山县| 绥江县| 志丹县| 麦盖提县|