com.discovery.darchrow.date.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.discovery.darchrow.date.DateUtil.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.discovery.darchrow.date;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.discovery.darchrow.text.DateFormatUtil;
import com.discovery.darchrow.util.Validator;

/**
 * 
 * Date ?(feilong-core ).
 * <p>
 * :
 * </p>
 * 
 * <ul>
 * 
 * <li>
 * <ul>
 * <li>{@link DateUtil#string2Date(String, String)}</li>
 * </ul>
 * </li>
 * 
 * <li>
 * <ul>
 * <li>{@link DateUtil#date2String(Date, String)}</li>
 * </ul>
 * </li>
 * 
 * <li>?
 * <ul>
 * <li>{@link DateUtil#addDay(Date, int)}</li>
 * <li>{@link DateUtil#addHour(Date, int)}</li>
 * <li>{@link DateUtil#addMinute(Date, int)}</li>
 * <li>{@link DateUtil#addMonth(Date, int)}</li>
 * <li>{@link DateUtil#addSecond(Date, int)}</li>
 * <li>{@link DateUtil#addWeek(Date, int)}</li>
 * <li>{@link DateUtil#addYear(Date, int)}</li>
 * </ul>
 * </li>
 * 
 * <li>?
 * <ul>
 * <li>{@link DateUtil#getDayOfMonth(Date)}</li>
 * <li>{@link DateUtil#getDayOfWeek(Date)}</li>
 * <li>{@link DateUtil#getDayOfYear(Date)}</li>
 * <li>{@link DateUtil#getHourOfDay(Date)}</li>
 * <li>{@link DateUtil#getHourOfYear(Date)}</li>
 * <li>{@link DateUtil#getMinute(Date)}</li>
 * <li>{@link DateUtil#getMonth(Date)}</li>
 * <li>{@link DateUtil#getSecond(Date)}</li>
 * <li>{@link DateUtil#getYear(Date)}</li>
 * <li>{@link DateUtil#getTime(Date)}</li>
 * </ul>
 * </li>
 * 
 * <li>
 * <ul>
 * <li>{@link DateUtil#getIntervalDay(long)}</li>
 * <li>{@link DateUtil#getIntervalDay(Date, Date)}</li>
 * <li>{@link DateUtil#getIntervalDay(String, String, String)}</li>
 * 
 * <li>{@link DateUtil#getIntervalWeek(long)}</li>
 * <li>{@link DateUtil#getIntervalWeek(Date, Date)}</li>
 * <li>{@link DateUtil#getIntervalWeek(String, String, String)}</li>
 * 
 * <li>{@link DateUtil#getIntervalHour(long)}</li>
 * <li>{@link DateUtil#getIntervalHour(Date, Date)}</li>
 * 
 * <li>{@link DateUtil#getIntervalMinute(long)}</li>
 * <li>{@link DateUtil#getIntervalSecond(long)}</li>
 * <li>{@link DateUtil#getIntervalSecond(Date, Date)}</li>
 * 
 * <li>{@link DateUtil#getIntervalTime(Date, Date)}</li>
 * </ul>
 * </li>
 * 
 * <li> {@link DateUtil#isLeapYear(int)}</li>
 * <li> {@link DateUtil#isEquals(Date, Date, String)}</li>
 * 
 * <li>
 * <ul>
 * <li>{@link DateUtil#isBefore(Date, String, String)}</li>
 * <li>{@link DateUtil#isBefore(String, String, String)}</li>
 * </ul>
 * </li>
 * 
 * <li>
 * <ul>
 * <li>{@link DateUtil#isInTime(Date, Date, Date)}</li>
 * <li>{@link DateUtil#isInTime(Date, String, String, String)}</li>
 * </ul>
 * </li>
 * 
 * </ul>
 * 
 * ,??:
 * 
 * <pre>
 * {@code
 * 
 * Date nextWeekDay = addDay(date, 7);
 * return getFirstDateOfThisWeek(nextWeekDay);
 * 
 * ?
 * Date nextWeekDay = addDay(date, 7);
 * return getLastDateOfThisWeek(nextWeekDay);
 * 
 * 
 * Date nextWeekDay = addDay(date, -7);
 * return getFirstDateOfThisWeek(nextWeekDay);
 * 
 * ?
 * Date nextWeekDay = addDay(date, -7);
 * return getLastDateOfThisWeek(nextWeekDay);
 * 
 * 
 * 
 * return DateUtil.getFirstDateOfThisMonth(DateUtil.addMonth(now, +1)));
 * ?
 * return DateUtil.getLastDateOfThisMonth(DateUtil.addMonth(now, +1)));
 * 
 * return DateUtil.getFirstDateOfThisMonth(DateUtil.addMonth(now, -1)));
 * ?
 * return DateUtil.getLastDateOfThisMonth(DateUtil.addMonth(now, -1)));
 * 
 * 
 * 
 * return DateUtil.getFirstDateOfThisYear(DateUtil.addYear(now, -1));
 * ?
 * return DateUtil.getLastDateOfThisYear(DateUtil.addYear(now, -1)));
 * 
 * return DateUtil.getFirstDateOfThisYear(DateUtil.addYear(now, +1));
 * ?
 * return DateUtil.getLastDateOfThisYear(DateUtil.addYear(now, +1)));
 * 
 * }
 * </pre>
 * 
 * @author feilong
 * @version 1.0.0 2010-1-27 ?01:53:21
 * @version 1.0.5 2014-5-6 10:04
 * @see CalendarUtil
 * @see DatePattern
 * @see DateFormatUtil
 * @see org.apache.commons.lang3.time.DateUtils
 * @see org.apache.commons.lang.time.DateUtils
 * @since 1.0.0
 */
public final class DateUtil {

    /** Don't let anyone instantiate this class. */
    private DateUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * (?<span style="color:red"></span>).
     * 
     * <pre>
     * <span style="color:red">-1,??? </span>
     * 
     *   2012-10-16 22:43:06 
     * return 2012-10-15 22:43:06.169
     * </pre>
     * 
     * @param date
     *            date
     * @return / ?
     * @see #toCalendar(Date)
     * @see Calendar#add(int, int)
     * @see Calendar#getTime()
     */
    public static Date getYesterday(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.add(Calendar.DATE, -1);
        return CalendarUtil.toDate(calendar);
    }

    // *****************************week****************************************************
    /**
     * date <span style="color:red"> ()</span> <code>00:00:00.000</code> .
     * <p>
     * ?:,<span style="color:red">,?</span><br>
     * ?:,?
     * </p>
     * 
     * <pre>
     *   2012-10-11 17:10:30.701(),
     * return 2012-10-07 00:00:00.000
     * 
     * 
     * getFirstDateOfThisWeek(2014-01-01 05:00:00)
     * return 2013-12-29 00:00:00.000
     * </pre>
     * 
     * @param date
     *            the date
     * @return date  <code>00:00:00.000</code> 
     * @see #toCalendar(Date)
     * @see Calendar#set(int, int)
     * @see CalendarUtil#resetDayBegin(Calendar)
     * @see Calendar#getTime()
     */
    public static Date getFirstDateOfThisWeek(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        CalendarUtil.resetDayBegin(calendar);
        return CalendarUtil.toDate(calendar);
    }

    /**
     * date <span style="color:red">?()</span> <code>23:59:59.999</code> .
     * 
     * <p>
     * ?:,<span style="color:red">,?</span><br>
     * ?:,?
     * </p>
     * 
     * <pre>
     *   2012-10-11 17:10:30.701 (),
     * return 2012-10-13 23:59:59.999
     * 
     * 
     * getLastDateOfThisWeek(2014-12-31 05:00:00)
     * return 2015-01-03 23:59:59.999
     * </pre>
     * 
     * @param date
     *            ?date
     * @return date ? <code>23:59:59.999</code> 
     * @see #toCalendar(Date)
     * @see Calendar#set(int, int)
     * @see CalendarUtil#resetDayEnd(Calendar)
     * @see Calendar#getTime()
     * @since 1.0.1
     */
    public static Date getLastDateOfThisWeek(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        CalendarUtil.resetDayEnd(calendar);
        return CalendarUtil.toDate(calendar);
    }

    // *********************************************************************************

    /**
     * ,<code>00:00:00</code> .
     * 
     * <p>
     *   2012-10-11 17:10:30.701 (),<br>
     * return 2012-10-01 00:00:00
     * </p>
     * 
     * @param date
     *            the date
     * @return Date
     * @see #toCalendar(Date)
     * @see Calendar#set(int, int)
     * @see CalendarUtil#resetDayBegin(Calendar)
     * @see Calendar#getTime()
     */
    public static Date getFirstDateOfThisMonth(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        CalendarUtil.resetDayBegin(calendar);
        return CalendarUtil.toDate(calendar);
    }

    /**
     * <span style="color:red">?</span> <code>23:59:59.999</code> .
     * <p>
     * ?,,2 2829
     * </p>
     * 
     * <pre>
     *   2012-10-11 17:10:30.701,
     * return 2012-10-31 23:59:59.999
     * </pre>
     * 
     * @param date
     *            the date
     * @return Date
     * @see #toCalendar(Date)
     * @see Calendar#set(int, int)
     * @see CalendarUtil#resetDayEnd(Calendar)
     * @see Calendar#getTime()
     */
    public static Date getLastDateOfThisMonth(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        CalendarUtil.resetDayEnd(calendar);
        return CalendarUtil.toDate(calendar);
    }

    /**
     * <span style="color:red"></span>,<code>00:00:00.000</code> .
     * 
     * <pre>
     *   2012-10-11 17:10:30.701,
     * return 2012-01-01 00:00:00
     * </pre>
     * 
     * @param date
     *            
     * @return date
     * @see #toCalendar(Date)
     * @see Calendar#set(int, int)
     * @see CalendarUtil#resetDayBegin(Calendar)
     * @see Calendar#getTime()
     */
    public static Date getFirstDateOfThisYear(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        CalendarUtil.resetDayBegin(calendar);
        return CalendarUtil.toDate(calendar);
    }

    /**
     *  <code>week</code>.
     * 
     * <pre>
     *   2015-7-29 14:08
     * 
     * DateUtil.getFirstWeekOfSpecifyDateYear(NOW, Calendar.FRIDAY)
     * return 2015-01-02 00:00:00.000
     * 
     * DateUtil.getFirstWeekOfSpecifyDateYear(NOW, Calendar.MONDAY)
     * return 2015-01-05 00:00:00.000
     * </pre>
     * 
     * @param date
     *            
     * @param week
     *             1 ?2 3 4 5 6 7, ? {@link Calendar#SUNDAY}, {@link Calendar#MONDAY}, {@link Calendar#TUESDAY},
     *            {@link Calendar#WEDNESDAY}, {@link Calendar#THURSDAY}, {@link Calendar#FRIDAY}, {@link Calendar#SATURDAY}
     * @return the first week of specify date year
     * @see Calendar#SUNDAY
     * @see Calendar#MONDAY
     * @see Calendar#TUESDAY
     * @see Calendar#WEDNESDAY
     * @see Calendar#THURSDAY
     * @see Calendar#FRIDAY
     * @see Calendar#SATURDAY
     * @since 1.3.0
     */
    public static Date getFirstWeekOfSpecifyDateYear(Date date, int week) {
        Calendar calendar = toCalendar(date);
        calendar.clear();
        calendar.set(Calendar.YEAR, getYear(date));
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
        calendar.set(Calendar.DAY_OF_WEEK, week);

        //DAY_OF_WEEK_IN_MONTH ? DAY_OF_WEEK ???
        // WEEK_OF_MONTH  WEEK_OF_YEAR ??? ? getFirstDayOfWeek()  getMinimalDaysInFirstWeek()

        //DAY_OF_MONTH 1  7  DAY_OF_WEEK_IN_MONTH 1
        //8  14  DAY_OF_WEEK_IN_MONTH 2?
        //DAY_OF_WEEK_IN_MONTH 0  DAY_OF_WEEK_IN_MONTH 1 ?
        //?? DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1
        //?????
        // 31  DAY_OF_WEEK_IN_MONTH -1  DAY_OF_WEEK_IN_MONTH 5  DAY_OF_WEEK_IN_MONTH 4 ??
        return CalendarUtil.toDate(calendar);
    }

    /**
     * <span style="color:red">?</span> <code>23:59:59.999</code> <br>
     * 
     * <pre>
     *   2012-10-11 17:10:30.701,
     * 
     * return 2012-12-31 23:59:59.999
     * </pre>
     * 
     * @param date
     *            ?date
     * @return Date
     * @see #toCalendar(Date)
     * @see Calendar#set(int, int)
     * @see CalendarUtil#resetDayEnd(Calendar)
     * @see Calendar#getTime()
     */
    public static Date getLastDateOfThisYear(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.MONTH, Calendar.DECEMBER);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        CalendarUtil.resetDayEnd(calendar);
        return CalendarUtil.toDate(calendar);
    }

    // [start]operate ?(?)

    /**
     * ,?( {@link java.util.Calendar#YEAR}?,??),,.
     * 
     * <pre>
     * addYear(2012-06-29 00:33:05,5)
     * return 20<span style="color:red">17</span>-06-29 00:33:05
     * 
     * addYear(2012-06-29 00:33:05,-5)
     * return 20<span style="color:red">07</span>-06-29 00:33:05
     * </pre>
     * 
     * @param date
     *            
     * @param year
     *             ? ??
     * @return ??
     * @see #operateDate(Date, int, int)
     * @see Calendar#YEAR
     * @see org.apache.commons.lang3.time.DateUtils#addYears(Date, int)
     */
    public static Date addYear(Date date, int year) {
        return operateDate(date, Calendar.YEAR, year);
    }

    /**
     * ?,( {@link java.util.Calendar#MONTH}?,??),,.
     * 
     * <pre>
     * addMonth(2012-10-16 23:12:43,5)
     * return 2013-03-16 23:12:43.932
     * 
     * addMonth(2012-10-16 23:12:43,-5)
     * return 2012-05-16 23:12:43.943
     * </pre>
     * 
     * @param date
     *            
     * @param month
     *            ?, <span style="color:red">?</span>,??<br>
     *            -3  3?
     * @return ??
     * @see #operateDate(Date, int, int)
     * @see Calendar#MONTH
     * @see org.apache.commons.lang3.time.DateUtils#addMonths(Date, int)
     */
    public static Date addMonth(Date date, int month) {
        return operateDate(date, Calendar.MONTH, month);
    }

    /**
     * ? ( {@link java.util.Calendar#DAY_OF_MONTH}?,??),,.
     * 
     * <pre>
     * addDay(2012-06-29 00:42:26,5)
     * return 2012-07-04 00:42:26
     * 
     * addDay(2012-06-29 00:42:26,-5)
     * return 2012-06-24 00:42:26
     * 
     * addDay(2014-12-31 02:10:05,5)
     * return 2015-01-05 02:10:05.000
     * 
     * addDay(2014-01-01 02:10:05,-5)
     * return 2013-12-27 02:10:05.000
     * </pre>
     * 
     * @param date
     *            
     * @param day
     *            ??,<span style="color:red">?</span>,??<br>
     * @return ?
     * @see #operateDate(Date, int, int)
     * @see Calendar#DAY_OF_MONTH
     * @see org.apache.commons.lang3.time.DateUtils#addDays(Date, int)
     */
    public static Date addDay(Date date, int day) {
        // Calendar.DAY_OF_MONTH  Calendar.DATE ??. 1.
        return operateDate(date, Calendar.DAY_OF_MONTH, day);
    }

    /**
     * ? ({@link Calendar#WEEK_OF_YEAR}?,??),,.
     * 
     * <pre>
     * Example 1:
     * addWeek(2012-06-29 00:45:18,5)
     * return 2012-08-03 00:45:18
     * 
     * Example 2:
     * addWeek(2012-06-29 00:45:18,-5)
     * return 2012-05-25 00:45:18
     * </pre>
     * 
     * @param date
     *            
     * @param week
     *            ??,<span style="color:red">?</span>,??<br>
     * @return ?
     * @see #operateDate(Date, int, int)
     * @see Calendar#WEEK_OF_YEAR
     */
    public static Date addWeek(Date date, int week) {
        return operateDate(date, Calendar.WEEK_OF_YEAR, week);
    }

    /**
     * ?? ({@link Calendar#HOUR_OF_DAY} 24??,??),,.
     * 
     * <pre>
     * addHour(2012-06-29 00:46:24,5)
     * return 2012-06-29 05:46:24
     * 
     * addHour(2012-06-29 00:46:24,-5)
     * return 2012-06-28 19:46:24
     * </pre>
     * 
     * <p>
     * {@link Calendar#HOUR}12?? <br>
     * {@link Calendar#HOUR_OF_DAY}24??
     * </p>
     * 
     * @param date
     *            the date
     * @param hour
     *            the hour,<span style="color:red">?</span>,??<br>
     * @return the date
     * @see #operateDate(Date, int, int)
     * @see Calendar#HOUR_OF_DAY
     * @see org.apache.commons.lang3.time.DateUtils#addHours(Date, int)
     */
    public static Date addHour(Date date, int hour) {
        return operateDate(date, Calendar.HOUR_OF_DAY, hour);
    }

    /**
     * ? ({@link Calendar#MINUTE}?,??),,.
     * 
     * <pre>
     * addMinute(2012-10-16 23:20:33,180)
     * return 2012-10-17 02:20:33.669
     * 
     * addMinute(2012-10-16 23:20:33,-180)
     * return 2012-10-16 20:20:33.669
     * </pre>
     * 
     * @param date
     *            the date
     * @param minute
     *            the minute,<span style="color:red">?</span>,??<br>
     * @return the date
     * @see #operateDate(Date, int, int)
     * @see Calendar#MINUTE
     * @see org.apache.commons.lang3.time.DateUtils#addMinutes(Date, int)
     */
    public static Date addMinute(Date date, int minute) {
        return operateDate(date, Calendar.MINUTE, minute);
    }

    /**
     * ? ({@link java.util.Calendar#SECOND}?,??),,.
     * 
     * <pre>
     * addSecond(2012-10-16 23:22:02,180)
     * return 2012-10-16 23:25:02.206
     * 
     * addSecond(2012-10-16 23:22:02,-180)
     * return 2012-10-16 23:19:02.206
     * </pre>
     * 
     * @param date
     *            ?
     * @param second
     *            ?,<span style="color:red">?</span>,??<br>
     * @return the date
     * @see #operateDate(Date, int, int)
     * @see Calendar#SECOND
     * @see org.apache.commons.lang3.time.DateUtils#addSeconds(Date, int)
     */
    public static Date addSecond(Date date, int second) {
        return operateDate(date, Calendar.SECOND, second);
    }

    /**
     * ?, ???.
     * 
     * @param currentDate
     *            ?date
     * @param field
     *            
     * @param amount
     *            ?,?
     * @return ? ???
     * @see #addYear(Date, int)
     * @see #addMonth(Date, int)
     * @see #addWeek(Date, int)
     * @see #addDay(Date, int)
     * @see #addHour(Date, int)
     * @see #addMinute(Date, int)
     * @see #addSecond(Date, int)
     * @see #toCalendar(Date)
     * @see Calendar#add(int, int)
     * @see org.apache.commons.lang3.time.DateUtils#add(Date, int, int)
     */
    public static Date operateDate(Date currentDate, int field, int amount) {
        Calendar calendar = toCalendar(currentDate);
        calendar.add(field, amount);
        return CalendarUtil.toDate(calendar);
    }

    // [end]

    // [start]fieldValue?
    /**
     * ? {@link java.util.Calendar#YEAR}.
     * 
     * <pre>
     * 2012-06-29
     * return 2012
     * </pre>
     * 
     * @param date
     *            the date
     * @return ?
     * @see CalendarUtil#getFieldValue(Date, int)
     * @see Calendar#YEAR
     */
    public static int getYear(Date date) {
        return CalendarUtil.getFieldValue(date, Calendar.YEAR);
    }

    /**
     * ?{@link java.util.Calendar#MONTH}<span style="color:red">(?+1?)</span>.
     * 
     * <pre>
     * 2012-06-29
     * return 6
     * </pre>
     * 
     * @param date
     *            the date
     * @return ?
     * @see CalendarUtil#getFieldValue(Date, int)
     * @see Calendar#MONTH
     */
    public static int getMonth(Date date) {
        return 1 + CalendarUtil.getFieldValue(date, Calendar.MONTH);
    }

    /**
     * ?{@link Calendar#WEEK_OF_YEAR}, 1,52(365/7=52.14).
     * 
     * <p>
     * ?:<br>
     * 20141-1 1-2 1-3 1-4 WEEK_OF_YEAR 1; <br>
     * 201412-28 12-29 12-30 12-31 WEEK_OF_YEAR 1
     * </p>
     * 
     * <pre>
     * 
     * Example 1:
     * 2014-06-03
     * return 23
     * 
     * Example 2:
     * 2014-01-01
     * return 1
     * 
     * Example 3:
     * 2014-12-29
     * return 23
     * 
     * Example 4:
     * 2014-12-20
     * return 51
     * 
     * Example 5:
     * 2014-12-26
     * return 52
     * </pre>
     * 
     * {@link Calendar#setMinimalDaysInFirstWeek(int)} ???,7?
     * 
     * <pre>
     * 
     * Example 1:
     * 2014-01-01
     * return 52
     * 
     * Example 3:
     * 2014-12-31
     * return 52
     * </pre>
     * 
     * ?,11??,?7,11?
     * 
     * @param date
     *            the date
     * @return ?
     * @see CalendarUtil#getFieldValue(Date, int)
     * @see Calendar#WEEK_OF_YEAR
     * @see Calendar#getFirstDayOfWeek()
     * @see Calendar#getMinimalDaysInFirstWeek()
     * @see Calendar#setMinimalDaysInFirstWeek(int)
     * @since 1.0.7
     */
    public static int getWeekOfYear(Date date) {
        return CalendarUtil.getFieldValue(date, Calendar.WEEK_OF_YEAR);
    }

    /**
     * ?().
     * 
     * <pre>
     * 2013-01-01
     * return 1
     * 
     * 2013-01-05
     * return 5
     * </pre>
     * 
     * @param date
     *            the date
     * @return ?()
     * @see CalendarUtil#getDayOfYear(int, int, int)
     * @see #getFirstDateOfThisYear(Date)
     * @see #getIntervalDay(Date, Date)
     * @since 1.0.2
     */
    public static int getDayOfYear(Date date) {
        Date firstDateOfThisYear = getFirstDateOfThisYear(date);
        return getIntervalDay(date, firstDateOfThisYear) + 1;
    }

    /**
     * ?{@link Calendar#DAY_OF_MONTH}.
     * 
     * <pre>
     * 2012-06-29
     * return 29
     * </pre>
     * 
     * @param date
     *            the date
     * @return ?
     * @see CalendarUtil#getFieldValue(Date, int)
     * @see Calendar#DAY_OF_MONTH
     */
    public static int getDayOfMonth(Date date) {
        return CalendarUtil.getFieldValue(date, Calendar.DAY_OF_MONTH);
    }

    /**
     * ?{@link Calendar#DAY_OF_WEEK}.
     * 
     * <p style="color:red">
     * ,1.
     * </p>
     * 
     * <pre>
     * SUNDAY?MONDAY?TUESDAY?WEDNESDAY?THURSDAY?FRIDAY  SATURDAY
     * 
     * 1-7
     * </pre>
     * 
     * <pre>
     * 2012-6-29  5
     * return 6
     * </pre>
     * 
     * @param date
     *            the date
     * @return ?
     * @see Calendar#SUNDAY
     * @see Calendar#MONDAY
     * @see Calendar#TUESDAY
     * @see Calendar#WEDNESDAY
     * @see Calendar#THURSDAY
     * @see Calendar#FRIDAY
     * @see Calendar#SATURDAY
     * @see CalendarUtil#getFieldValue(Date, int)
     * @see Calendar#DAY_OF_WEEK
     */
    public static int getDayOfWeek(Date date) {
        return CalendarUtil.getFieldValue(date, Calendar.DAY_OF_WEEK);
    }

    /**
     * ?(24?) {@link Calendar#HOUR_OF_DAY}.
     * 
     * <pre>
     * 2012-6-29 00:26:53
     * return 0
     * </pre>
     * 
     * @param date
     *            date
     * @return ?
     * @see CalendarUtil#getFieldValue(Date, int)
     * @see Calendar#HOUR_OF_DAY
     */
    public static int getHourOfDay(Date date) {
        return CalendarUtil.getFieldValue(date, Calendar.HOUR_OF_DAY);
    }

    /**
     * date  ?.
     * <p>
     * max value: 8784.
     * </p>
     * 
     * <pre>
     * 
     * 2013-01-01 00:00:05
     * return 0
     * 
     * 2013-01-01 01:00:05
     * return 1
     * 
     * 2013-01-05 12:00:05
     * return 108
     * 
     * 2013-09-09 17:28
     * return 6041
     * </pre>
     * 
     * @param date
     *            date
     * @return date  ?
     * @see #getFirstDateOfThisYear(Date)
     * @see #getIntervalHour(Date, Date)
     * @since 1.0.2
     */
    public static int getHourOfYear(Date date) {
        Date firstDateOfThisYear = getFirstDateOfThisYear(date);
        return getIntervalHour(firstDateOfThisYear, date);
    }

    /**
     *  {@link java.util.Calendar#MINUTE}.
     * 
     * <pre>
     * 2012-6-29 00:26:53
     * return 26
     * </pre>
     * 
     * @param date
     *            date
     * @return 
     * @see CalendarUtil#getFieldValue(Date, int)
     * @see Calendar#MINUTE
     */
    public static int getMinute(Date date) {
        return CalendarUtil.getFieldValue(date, Calendar.MINUTE);
    }

    /**
     * {@link java.util.Calendar#SECOND}.
     * 
     * <pre>
     * 2012-6-29 00:26:53
     * return 53
     * </pre>
     * 
     * @param date
     *            date
     * @return 
     * @see CalendarUtil#getFieldValue(Date, int)
     * @see Calendar#SECOND
     */
    public static int getSecond(Date date) {
        return CalendarUtil.getFieldValue(date, Calendar.SECOND);
    }

    /**
     * ,86400 {@link TimeInterval#SECONDS_PER_DAY}.
     * 
     * <pre>
     * 2013-09-09 16:42:41
     * return 60161
     * </pre>
     * 
     * @param date
     *            date
     * @return ?
     * @see TimeInterval#SECONDS_PER_DAY
     * @see TimeInterval#SECONDS_PER_HOUR
     * @see #getSecondOfHour(Date)
     * @since 1.0.2
     */
    public static int getSecondOfDay(Date date) {
        int hour = getHourOfDay(date);
        return hour * TimeInterval.SECONDS_PER_HOUR + getSecondOfHour(date);
    }

    /**
     * ??,3600 {@link TimeInterval#SECONDS_PER_HOUR}.
     * 
     * <pre>
     * 2013-09-15 01:15:23
     * return 923
     * </pre>
     * 
     * @param date
     *            date
     * @return ??
     * @see TimeInterval#SECONDS_PER_MINUTE
     * @see TimeInterval#SECONDS_PER_HOUR
     * @since 1.0.2
     */
    public static int getSecondOfHour(Date date) {
        int minute = getMinute(date);
        int second = getSecond(date);
        return second + minute * TimeInterval.SECONDS_PER_MINUTE;
    }

    /**
     *  1970  1  1  00:00:00 GMT ?, Date .
     * 
     * <pre>
     * 2012-6-29 00:28
     * return 1340900883288
     * </pre>
     * 
     * @param date
     *            date
     * @return date.getTime()
     */
    public static long getTime(Date date) {
        return date.getTime();
    }

    // [end]

    // [start]date2String/string2Date ?

    /**
     * ???.
     * 
     * <pre>
     * date2String(Tue Oct 16 23:49:21 CST 2012,DatePattern.commonWithMillisecond)
     * return 2012-10-16 23:49:21.525
     * </pre>
     * 
     * @param date
     *            ?
     * @param datePattern
     *            ? {@link DatePattern}
     * @return string
     * @see DateFormatUtil#format(Date, String)
     */
    public static String date2String(Date date, String datePattern) {
        return DateFormatUtil.format(date, datePattern);
    }

    /**
     * string??date.
     * 
     * @param dateString
     *            
     * @param datePattern
     *            ?,?{@link DatePattern}
     * @return string??date
     * @see DateFormatUtil#parse(String, String)
     */
    public static Date string2Date(String dateString, String datePattern) {
        return DateFormatUtil.parse(dateString, datePattern);
    }

    // [end]

    // [start]interval

    /**
     * .
     * 
     * @param spaceMillisecond
     *            
     * @return 
     * @see TimeInterval#MILLISECOND_PER_MINUTE
     */
    public static int getIntervalMinute(long spaceMillisecond) {
        return (int) (spaceMillisecond / (TimeInterval.MILLISECOND_PER_MINUTE));
    }

    /**
     * .
     * 
     * @param date1
     *            the date1
     * @param date2
     *            the date2
     * @return 
     * @see #getIntervalTime(Date, Date)
     * @see #getIntervalSecond(long)
     * @since 1.0.2
     */
    public static int getIntervalSecond(Date date1, Date date2) {
        long intervalTime = getIntervalTime(date1, date2);
        return getIntervalSecond(intervalTime);
    }

    /**
     * .
     * 
     * @param spaceMillisecond
     *            
     * @return 
     */
    public static int getIntervalSecond(long spaceMillisecond) {
        return (int) (spaceMillisecond / 1000);
    }

    /**
     * ?.
     * 
     * @param date1
     *            date1
     * @param date2
     *            date2
     * @return ?
     * @see #getIntervalTime(Date, Date)
     * @see #getIntervalHour(long)
     */
    public static int getIntervalHour(Date date1, Date date2) {
        long intervalTime = getIntervalTime(date1, date2);
        return getIntervalHour(intervalTime);
    }

    /**
     * ?.
     * 
     * @param spaceMillisecond
     *            
     * @return ?
     * @see TimeInterval#MILLISECOND_PER_HOUR
     */
    public static int getIntervalHour(long spaceMillisecond) {
        return (int) (spaceMillisecond / (TimeInterval.MILLISECOND_PER_HOUR));
    }

    /**
     * .
     *
     * @param date1
     *            the date1
     * @param date2
     *            the date2
     * @param datePattern
     *            pattern {@link DatePattern}
     * @return the interval week
     * @see #getIntervalWeek(Date, Date)
     * @since 1.2.1
     */
    public static int getIntervalWeek(String date1, String date2, String datePattern) {
        Date dateOne = string2Date(date1, datePattern);
        Date dateTwo = string2Date(date2, datePattern);
        return getIntervalWeek(dateOne, dateTwo);
    }

    /**
     * .
     *
     * @param date1
     *            the date1
     * @param date2
     *            the date2
     * @return the interval week
     * @see #getIntervalWeek(long)
     * @since 1.2.1
     */
    public static int getIntervalWeek(Date date1, Date date2) {
        long intervalTime = getIntervalTime(date1, date2);
        return getIntervalWeek(intervalTime);
    }

    /**
     * .
     *
     * @param spaceTime
     *            the space time
     * @return the interval week
     * @see TimeInterval#SECONDS_PER_WEEK
     * @since 1.2.1
     */
    public static int getIntervalWeek(long spaceTime) {
        return (int) (spaceTime / (TimeInterval.MILLISECOND_PER_WEEK));
    }

    //-******************getIntervalDay***************************************

    /**
     * .
     * 
     * @param date1
     *            date1
     * @param date2
     *            date2
     * @param datePattern
     *            ? {@link DatePattern}
     * @return 
     * @see #string2Date(String, String)
     * @see #getIntervalTime(Date, Date)
     * @see #getIntervalDay(long)
     */
    public static int getIntervalDay(String date1, String date2, String datePattern) {
        Date dateOne = string2Date(date1, datePattern);
        Date dateTwo = string2Date(date2, datePattern);
        return getIntervalDay(dateOne, dateTwo);
    }

    /**
     * .
     * 
     * @param date1
     *            date1
     * @param date2
     *            date2
     * @return 
     * @see #getIntervalTime(Date, Date)
     * @see #getIntervalDay(long)
     */
    public static int getIntervalDay(Date date1, Date date2) {
        long intervalTime = getIntervalTime(date1, date2);
        return getIntervalDay(intervalTime);
    }

    /**
     * .
     * 
     * @param spaceTime
     *            
     * @return 
     * @see TimeInterval#SECONDS_PER_DAY
     */
    public static int getIntervalDay(long spaceTime) {
        return (int) (spaceTime / (TimeInterval.MILLISECOND_PER_DAY));
    }

    /**
     * ,?date1?date2,? <span style="color:red">?</span>.
     * 
     * @param date1
     *            date1
     * @param date2
     *            date2
     * @return ,?date1?date2,??
     * @see #getTime(Date)
     * @see Math#abs(long)
     */
    public static long getIntervalTime(Date date1, Date date2) {
        return Math.abs(getTime(date2) - getTime(date1));
    }

    // [end]

    // [start]toCalendar

    /**
     * date?Calendar, {@link GregorianCalendar}.
     * 
     * <p>
     * {@link Calendar#getInstance()},???GregorianCalendar,<br>
     *  java.util.Calendar.createCalendar(TimeZone, Locale) ,<br>
     * Locale(), {@link GregorianCalendar#GregorianCalendar(java.util.TimeZone, java.util.Locale)} 
     * </p>
     * 
     * <h3>{@link GregorianCalendar}</h3>
     * 
     * <blockquote>
     * <p>
     * /,?""
     * </p>
     * </blockquote>
     *
     * @param date
     *            date
     * @return Calendar
     * @see Calendar#getInstance()
     * @see GregorianCalendar
     * @see Calendar#setTime(Date)
     * @see Calendar#setTimeInMillis(long)
     * 
     * @see org.apache.commons.lang3.time.DateUtils#toCalendar(Date)
     */
    public static Calendar toCalendar(Date date) {
        if (Validator.isNullOrEmpty(date)) {
            throw new NullPointerException("date can't be null/empty!");
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar;
    }

    // [end]

    // [start]isBefore 

    /**
     * ??,?Date, date_before ?date_after.
     * 
     * <p>
     * :
     * </p>
     * 
     * <pre>
     * isBefore("2011-05-01","2011-04-01",DateUtil.pattern_onlyDate)
     * return true
     * </pre>
     *
     * @param dateBefore
     *            the date before
     * @param dateAfter
     *            the date after
     * @param datePattern
     *            pattern {@link DatePattern}
     * @return date_before  date_after true
     * @see #string2Date(String, String)
     * @see #isBefore(Date, String, String)
     */
    public static boolean isBefore(String dateBefore, String dateAfter, String datePattern) {
        Date before = string2Date(dateBefore, datePattern);
        return isBefore(before, dateAfter, datePattern);
    }

    /**
     * ??,?Date, date_before ?date_after<br>
     * :
     * 
     * <pre>
     * isBefore("2011-05-01","2011-04-01",DateUtil.pattern_onlyDate)
     * return true
     * </pre>
     * 
     * @param before
     *            before
     * @param dateAfter
     *            dateAfter
     * @param datePattern
     *            pattern {@link DatePattern}
     * @return before  dateAfter true
     * @see #string2Date(String, String)
     * @see #isBefore(String, String, String)
     * @see Date#before(Date)
     */
    public static boolean isBefore(Date before, String dateAfter, String datePattern) {
        Date after = string2Date(dateAfter, datePattern);
        return isBefore(before, after);
    }

    /**
     * Checks if is before.
     *
     * @param before
     *            the before
     * @param when
     *            the after
     * @return true, if checks if is before
     * @see java.util.Date#before(Date)
     * @since 1.2.2
     */
    public static boolean isBefore(Date before, Date when) {
        return before.before(when);
    }

    /**
     * Checks if is after.
     *
     * @param after
     *            the after
     * @param when
     *            the when
     * @return true, if checks if is after
     * @since 1.2.2
     */
    public static boolean isAfter(Date after, Date when) {
        return after.after(when);
    }

    // [end]

    // [start]isInTime 

    /**
     * ??.
     * 
     * <pre>
     *  :2012-10-16 23:00:02
     * 
     * isInTime(date, "2012-10-10 22:59:00", "2012-10-18 22:59:00", DatePattern.commonWithTime)
     * 
     * return true
     * </pre>
     * 
     * @param date
     *            ?
     * @param beginTime
     *            
     * @param endTime
     *            ?
     * @param datePattern
     *            ??{@link DatePattern}
     * @return {@code  date after beginTimeDate&&?before endTimeDate,true}
     * @see #string2Date(String, String)
     * @see #isInTime(Date, Date, Date)
     */
    public static boolean isInTime(Date date, String beginTime, String endTime, String datePattern) {
        Date beginTimeDate = string2Date(beginTime, datePattern);
        Date endTimeDate = string2Date(endTime, datePattern);
        return isInTime(date, beginTimeDate, endTimeDate);
    }

    /**
     * ??.
     * 
     * <pre>
     *  :2012-10-16 23:00:02
     * 
     * isInTime(date, "2012-10-10 22:59:00", "2012-10-18 22:59:00")
     * 
     * return true
     * </pre>
     * 
     * @param date
     *            ?
     * @param beginTimeDate
     *            the begin time date
     * @param endTimeDate
     *            the end time date
     * @return {@code  date after beginTimeDate&&?before endTimeDate,true}
     * @see Date#after(Date)
     * @see Date#before(Date)
     */
    public static boolean isInTime(Date date, Date beginTimeDate, Date endTimeDate) {
        return date.after(beginTimeDate) && date.before(endTimeDate);
    }

    // [end]

    // [start]isEquals
    /**
     * ??,?.
     * 
     * @param date1
     *            1
     * @param date2
     *            2
     * @param datePattern
     *            ? {@link DatePattern}
     * @return true,?false
     * @see #date2String(Date, String)
     * @since 1.0.5 change name from isEqual to isEquals
     */
    public static boolean isEquals(Date date1, Date date2, String datePattern) {
        return date2String(date1, datePattern).equals(date2String(date2, datePattern));
    }

    // [end]

    // [start]isLeapYear 

    /**
     * ?? .
     * 
     * <p>
     * : {@code (year % 4 == 0 && year % 100 != 0) || year % 400 == 0}
     * </p>
     * 
     * <h3>:</h3>
     * 
     * <blockquote>
     * 
     * <p>
     * ??3655?4846?365.24219?tropical year.<br>
     * ?3650.242221366.<br>
     * </p>
     * 
     * <p>
     * ?40097.????0.0078?3?<br>
     * ???.
     * </p>
     * 
     * <p>
     * 400???400?,100?,<br>
     * ??.<br>
     * 
     * 20001900.<br>
     * </p>
     * </blockquote>
     * 
     * @param year
     *            
     * @return ??
     * @see GregorianCalendar#isLeapYear(int)
     */
    public static boolean isLeapYear(int year) {
        return new GregorianCalendar().isLeapYear(year);
    }

    // [end]
}