Home  >  Article  >  类库下载  >  Manipulate Java time safely and concisely using the apache.lang package

Manipulate Java time safely and concisely using the apache.lang package

高洛峰
高洛峰Original
2016-10-15 17:34:581835browse

Before that, let’s briefly talk about the time processing class of Java itself.

Date

Most of the APIs of Date are deprecated (outdated). The following are currently available

 
Date()                     代表执行到这句构造函数时的当前时间
 
Date(long d)               用一个相对于1970 年 1 月 1 日 00:00:00 以来的走过的毫秒数创建时间对象。
                           如:new Date()  等价于   new Date(System.currentTimeMillis())
 
boolean before(Date when)  当前时间对象是否早于 when
boolean after(Date when)   当前时间对象是否晚于 when
toString()
hashCode()
equals()

Calender

The Calendar class is an abstract class that associates a specific moment with a set of items such as YEAR, MONTH, DAY_OF_MONTH Methods are provided for converting between calendar fields such as HOUR and HOUR, and for manipulating calendar fields (such as getting next week's date). An instant can be expressed as a millisecond value, which is the offset from the epoch (00:00:00.000 January 1, 1970 GMT, Gregorian calendar).

This class also provides additional fields and methods for implementing specific calendar systems outside the scope of the package. These fields and methods are defined as protected.

Please see the API manual for details.

SimpleDateFromat

SimpleDateFromat is not thread-safe because it inherits a Calendar member in DateFormat, and every time the format operation is performed, it will be changed to the state of the member calendar. This is the source of insecurity.

Under multi-threading, it is very likely that the writing and reading operations on Calendar are not synchronized (not executed by the same thread), and an accident will occur.

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);
      //....

        //....
       

  }
}

Let’s start by introducing the protagonist category of the article.

DateUtils

provides operations on time objects, just like int operations.

Tool class, no instance creation is allowed

//-------------------静态字段-------------------
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

A tool class to convert time into a string. Uninstantiable objects.

Thread safe.

All overloaded formats in this class essentially adjust the following two functions. These two functions borrow the API of FastDateFormat.

FastDateFormat is the core class of apache time util that is better than Java SimpleDateFormat. It is thread safe.

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 internally utilizes Java's own SimpleDateFormat when parse (even so, DateUtils operations are thread-safe, because SimpleDateFromat is used as a local variable of the method), and DateFormatUtils utilizes the thread-safety developed by Apache FastDateFromat. Therefore, DateUtils and DateFormatUtils can satisfy simple time operations. If you need more customized operations, you may need the FastDateFormat introduced below.

FastDateFormat

FastDateFormat is a fast and thread-safe time operation class, which can completely replace SimpleDateFromat.

Because it is thread-safe, you can use it as a static field of a class

The constructor is protected and does not allow direct construction of its object, which can be obtained through the factory method.

The reason why FastDateFormat is thread-safe is because this class is stateless: the internal members are initialized during construction, and during the object lifetime, no API is provided for the outside world to modify them.

FastDateFormat has two very important objects inside:

FastDateParser

FastDatePrinter

completes parsing and formatting work respectively. They are also thread-safe and modified to final. Those who are interested can read the source code.

静态字段
 
用于构造时,控制时间或者日期显示的完整性,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()

Example:

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 time string expression pattern definition table

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn