http://www.blogjava.net/coolingverse/articles/148135.html

好了,来看我在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。

commons-lang(time应用)相关推荐

  1. org.apache.commons.lang.StringUtils的jar包是什么

    org.apache.commons.lang.StringUtils的jar包是什么 commons-lang-2.5.jar

  2. org.apache.commons.lang.StringUtils

    org.apache.commons.lang.StringUtils 作为jdk中lang包的补充 检查CharSequence是否为空,null或者空格 CharSequence (CharBuf ...

  3. org.springframework.uti包下的StringUtils的使用和org.apache.commons.lang包下StringUtils的使用

    一.org.springframework.util.StringUtils StringUtils常用方法 描述 boolean isEmpty(Object str) 判断字符串是否为空,如果为n ...

  4. guava和commons_使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在...

    guava和commons 最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,&quo ...

  5. 使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在

    最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,"一种检查变量是否包含诸如 ...

  6. Apache Commons Lang StringUtils

    因此,认为最好谈论我喜欢的另一个Java库. 它已经存在了一段时间,也许不是最令人兴奋的库,但是它非常有用. 我可能每天都使用它. org.apache.commons.lang.StringUtil ...

  7. java commons lang 随机数_Apache Common-lang组件里随机数工具类RandomStringUtils的一个bug...

    现在本文也转到了我自己的博客上,地址:月城小馆 Apache Common组件是java开发中常用的工具,其中的common-lang包是java基本数据类型的处理工具,包括数字.字符串.日期时间等多 ...

  8. apache.commons.lang.StringUtils 使用心得

    apache.commons.lang.StringUtils 使用心得 转载于:https://www.cnblogs.com/qinglizlp/p/5549687.html

  9. apache commons lang架包介绍

    commons lang组件介绍和学习 介绍 Java语言开发时有一个隐患,那就是java支持null值,这就导致很多时候操作可能会出异常. 因此很多第三方组件都会提供安全null safe 操作(即 ...

  10. org.apache.commons.lang.StringUtils中常用的方法

    org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...

最新文章

  1. android7.1 动态申请权限改为默认授权,修改PackageManagerService.java下的grantPermissions为true
  2. bootstrap --- 面板
  3. 信号模型噪声服从零均值高斯分布_非高斯噪声下基于分数低阶循环谱的调制识别方法...
  4. 数据结构之二叉树的逻辑结构和遍历
  5. Python:如何获取一个用户名的组ID
  6. JS记坑 ----- 在父节点中添加删除子节点
  7. dns服务器对网速有影响吗,更换DNS服务器可以提高网速吗?
  8. Kubernetes Secrets 详解
  9. Resource leak解决办法
  10. 植物大战僵尸_修改存档和金钱
  11. 【硬见小百科】SMT工艺,是什么影响锡膏印刷的质量
  12. 财务分析之资产负债表
  13. 将一个32bit的数据,颠倒过来,bit0与bit31交换,bit1与bit30交换,C语言实现与verilog实现。
  14. 「云计算」全球最大的5家云计算公司
  15. 1.1 Python中turtle的使用(画奥运五环)
  16. [下载]《SAP R/3 IDES 4.71 中文版》
  17. 【完整的WebGIS教程】7.1 ArcGIS API for JS行政区划导航(上)
  18. 计算机应用模块等级考试大纲,计算机等级考试大纲最新.doc
  19. html语音输入功能讯飞,win10系统利用讯飞语音输入法实现电脑语音输入的方案介绍...
  20. 基于51单片机的大棚环境土壤湿度光强监测系统proteus仿真原理图PCB

热门文章

  1. 841. Keys and Rooms 钥匙和房间
  2. Python+Hive环境搭建
  3. Linux 搜狗输入法 候选字乱码
  4. 2016年第七届蓝桥杯 - 省赛 - C/C++大学A组 - A. 网友年龄
  5. 大数据WEB阶段 (六)MySql详解(一)
  6. ubuntu下超级用户和普通用户
  7. 计算机视觉、机器学习、人工智能领域知识汇总
  8. 【C语言】控制台窗口图形界面编程(七):鼠标事件
  9. 【Linux】一步一步学Linux——getfacl命令(118)
  10. 【Linux】一步一步学Linux——fgrep命令(了解)(51)