本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7597551
建立时间:2006年5月29日




[Apache(jakarta)][Jakarta Commons笔记] org.apache.commons.lang.time
软件技术

lhwork 发表于 2006/8/11 17:16:29

好了,来看我在Common Lang中最后要讲的一个包:org.apache.commons.lang.time。这个包里面包含了如下5个类:   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.   具体的调用细节和完整的API请参阅Commons Lang的Javadoc。


阅读全文(1719) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.332 second(s), page refreshed 144771555 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号