org.apache.commons.lang.time用法实例

org.apache.commons.lang.time.DateUtils(常用的一些函数)
static java.util.Date addDays(java.util.Date date,int amount) 
 //Adds a number of days to a date returning a new object.
//一个日期对象+n天
static java.util.Date addHours(java.util.Date date,int amount)
   //Adds a number of hours to a date returning a new object.
//一个日期对象+n小时
static java.util.Date addMilliseconds(java.util.Date date,int amount) 
 //Adds a number of milliseconds to a date returning a new object.
//一个日期对象+n毫秒
static java.util.Date addMinutes(java.util.Date date,int amount) 
 //Adds a number of minutes to a date returning a new object.
//一个日期对象+n分
static java.util.Date addMonths(java.util.Date date,int amount) 
 //Adds a number of months to a date returning a new object.
//一个日期对象+n月
static java.util.Date addSeconds(java.util.Date date,int amount)
   //Adds a number of seconds to a date returning a new object.
   //一个日期对象+n秒
static java.util.Date addWeeks(java.util.Date date,int amount) 
 //Adds a number of weeks to a date returning a new object.
//一个日期对象+N个星期
static java.util.Date addYears(java.util.Date date,int amount) 
 //Adds a number of years to a date returning a new object.
//一个日期对象+N年
static boolean isSameDay(java.util.Date date1,java.util.Date date2) 
 //Checks if two date objects are on the same day ignoring time.
//两个日期对象是否相等(只比较年-月-日)
static boolean isSameInstant(java.util.Date date1,java.util.Date date2) 
 //Checks if two date objects represent the same instant in time.
//比较两个日期是否完全相等(精确到毫秒)
static java.util.Date parseDate(java.lang.String str,java.lang.String[] parsePatterns)
 //Parses a string representing a date by trying a variety of different parsers.
 //例 1:
 String[] format={"yyyy-MM-dd"};
System.out.println(DateUtils.parseDate("2009-10-20",format));
2:
new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.addMilliseconds(new Date(), 7000))


org.apache.commons.lang.time。这个包里面包含了如下5个类:

 

DateFormatUtils – 提供格式化日期和时间的功能及相关常量;

DateUtils – CalendarDate的基础上提供更方便的访问;

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.


你可能感兴趣的:(#,Java相关)