使用apache.lang包安全简洁地操作Java时间
引言
最近偶遇apache开发的工作java工具包,一使用,就发现自己爱上它了。不多说了,下面介绍org.apache.commons.lang3.time包中处理java程序员为之头疼的时间的类。
附上官网jar包下载地址:http://commons.apache.org/proper/commons-lang/download_lang.cgi
在这之前还是先简单说一下java本身的时间处理类。
Date
Calender
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
该类还为实现包范围外的具体日历系统提供了其他字段和方法。这些字段和方法被定义为 protected。
具体可见API手册。
SimpleDateFromat
SimpleDateFromat不是线程安全的,这是因为它继承了DateFormat中的一个Calendar成员,每次在执行format操作时,都会改成成员calendar的状态。这就是不安全的根源。
多线程下,很可能对Calendar的写 和 读 操作不同步(不是被同一个线程执行的),就会发生意外。
class DateFromat
{
protected Calendar calendar;
//...
} SimpleDateFromat extends DateFromat
{ private StringBuffer format(Date date, StringBuffer toAppendTo,FieldDelegate delegate)
{
// Convert input date to time field list
calendar.setTime(date);
//.... //.... }
}
下面开始介绍文章的主角类。
DateUtils
提供了对时间对象的运算操作,就像和操作 int 一样。
工具类,不允许创建实例
//-------------------静态字段-------------------
public static final long MILLIS_PER_DAY = 86400000L 一天的毫秒数
public static final long MILLIS_PER_HOUR = 3600000L 一个小时的毫秒数
public static final long MILLIS_PER_MINUTE = 60000L 一分钟的毫秒数
public static final long MILLIS_PER_SECOND = 1000L 一秒钟的毫秒数
//-------------------静态方法------------------
Date的运算和修改
static Date addDays(Date date, int amount) 返回一个date 时间对象 添加 amount 天 后的新的Date 对象
static Date addHours(Date date, int amount) 返回一个date 时间对象 添加 amount h 后的新的Date 对象
static Date addMilliseconds(Date date, int amount) 返回一个date 时间对象 添加 amount 毫秒 后的新的Date 对象
static Date addMinutes(Date date, int amount) 返回一个date 时间对象 添加 amount 分钟 后的新的Date 对象
static Date addMonths(Date date, int amount) 返回一个date 时间对象 添加 amount 月 后的新的Date 对象
static Date addSeconds(Date date, int amount) 返回一个date 时间对象 添加 amount 秒 后的新的Date 对象
static Date addWeeks(Date date, int amount) 返回一个date 时间对象 添加 amount 周 后的新的Date 对象
static Date addYears(Date date, int amount) 返回一个date 时间对象 添加 amount 年 后的新的Date 对象
static Date setDays(Date date, int amount) 修改一个Date 对象的 天数 并返回新的Date对象。
static Date setHours(Date date, int amount) 修改一个Date 对象的 小时字段并返回新的Date
static Date setMilliseconds(Date date, int amount) 修改一个Date 对象的 毫秒,并返回新的Date 对象
static Date setMinutes(Date date, int amount) 修改一个Date 对象的 分钟
static Date setMonths(Date date, int amount) 修改月份
static Date setSeconds(Date date, int amount) 修改秒
static Date setYears(Date date, int amount) 修改 年
字符串------>Date
从一个字符串str中按照 给定的字符串时间格式(见文章最后的SimpleDateFormat表),解析出一个时间对象。
可以给定多个字符串时间格式,依次尝试解析,如果都不能正确解析,则抛出java.text.ParseException异常。
如
DateUtils.parseDate("10-05-2016 12:45",Locale.CHINA, "dd-MM-yyyy HH:mm")
static Date parseDate(String str, Locale locale, String... parsePatterns)
static Date parseDate(String str, String... parsePatterns)
static Date parseDateStrictly(String str, Locale locale, String... parsePatterns)
static Date parseDateStrictly(String str, String... parsePatterns)
对时间的向上取整,截断,四舍五入,和 对浮点数的操作机制一样。
对一个时间对象的某个字段进行向上取整。 filed指定取整的字段,可以取的值为
Calendar.SECOND
Calendar.MINUTE
Calendar.HOUR_OF_DAY
Calendar.DAY_OF_MONTH
Calendar.MONTH
Calendar.YEAR 等...
如时间为:2016-2-12 22:17:48,若对年进行向上取整(ceiling),则看传入的参数的月份,为2,向上取值为年底,则会变为2017年
2017-1-1 0:00:00
如时间为:2016-2-25 22:17:48,若对月进行截断取整(truncate),则看传入的参数的天,为12,直接丢弃,则会变为本月第一天
2016-2-1 0:00:00
static Calendar ceiling(Calendar date, int field)
static Date ceiling(Date date, int field)
static Calendar round(Calendar date, int field) 和ceil同理,round 是四舍五入
static Date round(Date date, int field)
static Calendar truncate(Calendar date, int field) 对一个时间对象的某个字段进行截断。
static Date truncate(Date date, int field)
static int truncatedCompareTo(Calendar cal1, Calendar cal2, int field) 2个时间对象截断某个字段后比较,前者小返回-1,相同返回0,否则返回1
static int truncatedCompareTo(Date date1, Date date2, int field)
static boolean truncatedEquals(Calendar cal1, Calendar cal2, int field) 2个时间对象截断某个字段后比较,相同返回true,否则返回false
static boolean truncatedEquals(Date date1, Date date2, int field)
将Date 转换为Calendar
static Calendar toCalendar(Date date)
时间 对象的 想等/相近 比较
static boolean isSameDay(Calendar cal1, Calendar cal2) 是否是同一天,而不在乎具体时间,如 2月12 18:00 和 2月12 23:35 都是一天
static boolean isSameDay(Date date1, Date date2) 是否是同一天,而不在乎具体时间,如 2月12 18:00 和 2月12 23:35 都是一天
static boolean isSameInstant(Calendar cal1, Calendar cal2) 是否完全代表同一个时刻,相同。
static boolean isSameInstant(Date date1, Date date2) 是否完全代表同一个时刻,相同。
DateFormatUtils
将时间转化为字符串的工具类。不可实例化对象。
线程安全。
这个类中的所有重载的format 实质都是调了下面2个函数。而这2个函数中又借用了FastDateFormat的API。
FastDateFormat是apache time util 优于Java SimpleDateFormat 的核心类。它是线程安全的。
public static String format(final Date date, final String pattern, final TimeZone timeZone, final Locale locale) {
final FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, locale);
return df.format(date);
}
public static String format(final Calendar calendar, final String pattern, final TimeZone timeZone, final Locale locale) {
final FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, locale);
return df.format(calendar);
}
时间参数 可以是Date 对象 ,Calender 对象 ,或者一个相对于1970年的long整数 pattern ,如:"yyyy-MM-dd HH:mm:ss" 参见文章最后SimpleDateFormat格式表 Locale:地理,政治和文化地区 如Locale.CHINA TimeZone:时区偏移量.
TimeZone.getTimeZone("GMT+:08:00"); 北京时间
TimeZone.getDefault() 默认 static String format(Calendar calendar, String pattern) static String format(Calendar calendar, String pattern, Locale locale) static String format(Calendar calendar, String pattern, TimeZone timeZone) static String format(Calendar calendar, String pattern, TimeZone timeZone, Locale locale) static String format(Date date, String pattern) static String format(Date date, String pattern, Locale locale) static String format(Date date, String pattern, TimeZone timeZone) static String format(Date date, String pattern, TimeZone timeZone, Locale locale) static String format(long millis, String pattern) static String format(long millis, String pattern, Locale locale) static String format(long millis, String pattern, TimeZone timeZone) static String format(long millis, String pattern, TimeZone timeZone, Locale locale) static String formatUTC(Date date, String pattern) static String formatUTC(Date date, String pattern, Locale locale) static String formatUTC(long millis, String pattern) static String formatUTC(long millis, String pattern, Locale locale)
DateUtils 在parse时内部利用了java自身的SimpleDateFormat(即便如此,DateUtils的操作都是是线程安全的,因为SimpleDateFromat是作为方法的局部变量使用的),而 DateFormatUtils 利用了apache开发的线程安全的FastDateFromat。因此,DateUtils和DateFormatUtils可以满足简单的时间操作了。如果需要更多的定制化操作,就可能需要
下面介绍的FastDateFormat了。
FastDateFormat
FastDateFormat是一个快速 且 线程安全的时间操作类,它完全可以替代SimpleDateFromat。
因为是线程安全的,所以你可以把它作为一个类的静态字段使用
构造函数为protected,不允许直接构造它的对象,可以通过工厂方法获取。
FastDateFormat之所以是线程安全的,是因为这个类是无状态的:内部的成员在构造时就完成了初始化,并在对象存活期,不提供任何API供外界修改他们。
FastDateFormat内部有很重要的2个对象:
分别完成解析和format工作。他们也都是线程安全的,都修饰为final。有兴趣的可以取读源代码。
静态字段
用于构造时,控制时间或者日期显示的完整性,FULL最完整,SHORT最次。
static int FULL 表示完全显示
static int LONG
static int MEDIUM
static int SHORT
构造
static FastDateFormat getDateInstance(int style)
static FastDateFormat getDateInstance(int style, Locale locale)
static FastDateFormat getDateInstance(int style, TimeZone timeZone)
static FastDateFormat getDateInstance(int style, TimeZone timeZone, Locale locale)
只控制日期显示格式
使用style指定的日期显示的完整性,静态字段提供
timeZone 指定时区,若不指定,则使用系统默认的
locale 指定 国家区域,若不指定,则使用系统默认的
static FastDateFormat getInstance()
static FastDateFormat getInstance(String pattern)
static FastDateFormat getInstance(String pattern, Locale locale)
static FastDateFormat getInstance(String pattern, TimeZone timeZone)
static FastDateFormat getInstance(String pattern, TimeZone timeZone, Locale locale)
通过String类型的pattern自定义显示格式
String类型的pattern 指定format格式,参见SimpleDateFormat
timeZone 指定时区,若不指定,则使用系统默认的
locale 指定 国家区域,若不指定,则使用系统默认的
static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle)
static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone)
static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone, Locale locale)
同时控制 日期和 时间的显示格式
使用style指定的日期和时间显示的完整性,静态字段提供
StringBuffer format(Date date, StringBuffer buf)
StringBuffer format(long millis, StringBuffer buf)
StringBuffer format(Calendar calendar, StringBuffer buf)
将格式化后的字符串写入到一个StringBuffer对象中
String format(long millis)
String format(Date date)
String format(Calendar calendar)
Date parse(String source)
Date parse(String source, ParsePosition pos)
从字符串解析一个Date,解析的模式是构造时决定的
String getPattern() 获取fommat时的pattern
TimeZone getTimeZone()
Locale getLocale()
例子:
import java.util.Date;
import java.util.Locale; import org.apache.commons.lang3.time.FastDateFormat; /**
* 当使用FastDateFormat.getInstance()构造时,需要和SimpleDateFomat一样,自定义格式化字符串。
* 当使用FastDateFormat.getDateTimeInstance() 构造时,需要 FastDateFormat的4个静态字段指定日期 和 时间显示的具体程度
* 当使用FastDateFormat.getDateInstance() 构造时,意为着你只想显示日期,需要 FastDateFormat的4个静态字段指定日期的显示的具体程度
*
*/ public class Test { public static void showCustom()
{ String pattern = "yyyy-MM-dd HH:mm:ss"; final FastDateFormat df = FastDateFormat.getInstance(pattern); System.out.println(df.format(new Date())); } public static void showDateAndTime()
{
final FastDateFormat df = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL,
FastDateFormat.FULL,
Locale.CHINA);
System.out.println(df.format(new Date())); }
public static void showDate()
{
final FastDateFormat df = FastDateFormat.getDateInstance(FastDateFormat.LONG, Locale.CHINA); System.out.println(df.format(new Date())); } public static void main(String[] args)
{ showCustom(); showDateAndTime(); showDate(); /*Output
*
* 2016-10-15 16:18:49
2016年10月15日 星期六 下午04时18分49秒 CST
2016年10月15日 */ } }
SimpleDateFormat的时间字符串表达模式定义表
| Letter | Date or Time Component | Presentation | Examples |
|---|---|---|---|
G |
Era designator | Text | AD |
y |
Year | Year | 1996; 96 |
Y |
Week year | Year | 2009; 09 |
M |
Month in year (context sensitive) | Month | July; Jul; 07 |
L |
Month in year (standalone form) | Month | July; Jul; 07 |
w |
Week in year | Number | 27 |
W |
Week in month | Number | 2 |
D |
Day in year | Number | 189 |
d |
Day in month | Number | 10 |
F |
Day of week in month | Number | 2 |
E |
Day name in week | Text | Tuesday; Tue |
u |
Day number of week (1 = Monday, ..., 7 = Sunday) | Number | 1 |
a |
Am/pm marker | Text | PM |
H |
Hour in day (0-23) | Number | 0 |
k |
Hour in day (1-24) | Number | 24 |
K |
Hour in am/pm (0-11) | Number | 0 |
h |
Hour in am/pm (1-12) | Number | 12 |
m |
Minute in hour | Number | 30 |
s |
Second in minute | Number | 55 |
S |
Millisecond | Number | 978 |
z |
Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z |
Time zone | RFC 822 time zone | -0800 |
X |
Time zone | ISO 8601 time zone | -08; -0800; -08:00 |
使用apache.lang包安全简洁地操作Java时间的更多相关文章
- apache commons lang包中的StringUtils
计算一个字符串某个字符的出现次数 a, 使用charAt方法截取之后,循环判断. b, 使用apache commons lang包中的StringUtils: int n = StringUtils ...
- Apache -Common-lang包使用
原文:http://weigang-gao.iteye.com/blog/2188739 ArrayUtils – 用于对数组的操作,如添加.查找.删除.子数组.倒序.元素类型转换等: BitFiel ...
- JDK框架简析--java.lang包中的基础类库、基础数据类型
题记 JDK.Java Development Kit. 我们必须先认识到,JDK不过,不过一套Java基础类库而已,是Sun公司开发的基础类库,仅此而已,JDK本身和我们自行书写总结的类库,从技术含 ...
- Java语言Lang包下常用的工具类介绍_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 无论你在开发哪中 Java 应用程序,都免不了要写很多工具类/工具函数.你可知道,有很多现成的工具类可用,并且代码质量都 ...
- java.lang包
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.特性——不用import 2.String String x = "abc"; < ...
- java基础 lang包 详细介绍
Java.javax和org.其中以java开头的包名是JDK的基础语言包,以javax开头的属 (org是organization的简写).而在JDK API中还包含了一些以com.sun开头的包名 ...
- Java.lang 包 (包装类、String类、Math类、Class类、Object类)
Java 的核心 API(Application Programming Interface)是非常庞大的,这给开发者带来了很大的方便. java.lang 包是 Java 的核心类库,它包含了运行 ...
- Java.lang包的接口解读
Java.lang包中提供了八个接口: 1.Appendable 能够被追加 char 序列和值的对象.如果某个类的实例打算接收来自 Formatter的格式化输出,那么该类必须实现 Appendab ...
- 简单看看jdk7源码之java.lang包01
从今天开始简单开始读一遍jdk的源码,估计这个时间会很长,慢慢啃吧....(首先说一句抱歉,因为很多图都是直接百度扣的,图太多了不能为每一个图附上原版链接,不好意思!) 在网上看了很多的教程,读源码有 ...
随机推荐
- memcache/redis 缓存学习笔记
0.redis和memcache的区别 a.redis可以存储除了string之外的对象,如list,hash等 b.服务器宕机以后,redis会把内存的数据持久化到磁盘上,而memcache则不会 ...
- Linux chkconfig 命令详解
一.简介 chkconfig 命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 二.安装&启动 us ...
- 【BZOJ3450】Tyvj1952 Easy 期望DP
[BZOJ3450]Tyvj1952 Easy Description 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化一下这个游戏的规则有n次点击要做,成功了就是 ...
- BZOJ4260 Codechef REBXOR 题解
题目大意: 有一个长度为n的序列,求1≤l1≤r1<l2≤r2≤n使得(⊕r1i=l1ai)+(⊕r2i=l2ai)最大,输出这个最大值. 思路: 用Trie求出前缀异或和以及后缀异或和,再求出 ...
- 目前quanben评十大哲学家
排名分先后,包含时间.地区和主要正面成就以及其他代表人物. 1. 伊曼努尔·康德:十八世纪:东普鲁士:理性的界限,现象和本体分立以及主体的地位2. 柏拉图:公元前三四百年:古希腊:理想(念)世界3. ...
- Android AIDL 进行进程间通讯(IPC)
编写AIDL文件时,需要注意: 1.接口名和aidl文件名相同. 2.接口和方法前不用加访问权限修饰符 (public.private.protected等,也不能用final.static). 3. ...
- ACM 会场安排问题
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- jQuery中的Ajax几种请求方式
1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (String) : 请求的HTML页的URL地址. data (M ...
- 转:js-sdk探索之微信网页分享
原文地址 微信是一个很不错的传播平台,最近公司需要做一个新年贺卡,使用html5制作一个很小的动画,然后发送给客户,不需要和后台有任何的联系,一个很简单的功能,需要利用微信的分享功能,毕竟微信分享的带 ...
- 【POJ】1284 Primitive Roots
http://poj.org/problem?id=1284 题意:求一个素数p的原根个数.(p<=65535) #include <cstdio> #include <cst ...