com.feilong.commons.core.date.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.commons.core.date.DateUtil.java

Source

/*
 * Copyright (C) 2008 feilong (venusdrogon@163.com)
 *
 * 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.feilong.commons.core.date;

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

import com.feilong.commons.core.text.DateFormatUtil;
import com.feilong.commons.core.util.Validator;

/**
 * <p>
 * Date ?(feilong-core ).
 * </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#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 <a href="venusdrogon@163.com"></a>
 * @version 1.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!");
    }

    /**
     * (?)
     * 
     * <pre>
     * -1,??? 
     * 
     *   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()
     * @since 1.0
     */
    public static final Date getYesterday(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    // *****************************week****************************************************
    /**
     * date  (<b></b>) <code>00:00:00.000</code> <br>
     * ?:,,?<br>
     * ?:,?
     * 
     * <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 #dayBegin(Calendar)
     * @see Calendar#getTime()
     * @since 1.0
     */
    public static final Date getFirstDateOfThisWeek(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        dayBegin(calendar);
        return calendar.getTime();
    }

    /**
     * date ?() <code>23:59:59.999</code> <br>
     * ?:,,?<br>
     * ?:,?
     * 
     * <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 #dayEnd(Calendar)
     * @see Calendar#getTime()
     * @since 1.0.1
     */
    public static final Date getLastDateOfThisWeek(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        dayEnd(calendar);
        return calendar.getTime();
    }

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

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

    /**
     * ? <code>23:59:59.999</code> <br>
     * ?,,2 2829
     * 
     * <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 #dayEnd(Calendar)
     * @see Calendar#getTime()
     * @since 1.0
     */
    public static final Date getLastDateOfThisMonth(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        dayEnd(calendar);
        return calendar.getTime();
    }

    /**
     * ,<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 #dayBegin(Calendar)
     * @see Calendar#getTime()
     * @since 1.0
     */
    public static final Date getFirstDateOfThisYear(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        dayBegin(calendar);
        return calendar.getTime();
    }

    /**
     * ? <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 #dayEnd(Calendar)
     * @see Calendar#getTime()
     * @since 1.0
     */
    public static final Date getLastDateOfThisYear(Date date) {
        Calendar calendar = toCalendar(date);
        calendar.set(Calendar.MONTH, Calendar.DECEMBER);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        dayEnd(calendar);
        return calendar.getTime();
    }

    // [start]operate ?(?)
    /**
     * ,?(?,??)<br>
     * ,.
     * 
     * <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
     * @since 1.0
     */
    public static final Date addYear(Date date, int year) {
        return operateDate(date, Calendar.YEAR, year);
    }

    /**
     * ?,(?,??)<br>
     * ,.
     * 
     * <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
     *            ? ? ??<br>
     *            -3  3?
     * @return ??
     * @see #operateDate(Date, int, int)
     * @see Calendar#MONTH
     */
    public static final Date addMonth(Date date, int month) {
        return operateDate(date, Calendar.MONTH, month);
    }

    /**
     * ? (?,??)<br>
     * ,.
     * 
     * <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
     *            ?? ?
     * @return ?
     * @see #operateDate(Date, int, int)
     * @see Calendar#DAY_OF_MONTH
     * @since 1.0
     */
    public static final 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}(week?,??)<br>
     * ,.
     * 
     * <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
     *            ?? ?
     * @return ?
     * @see #operateDate(Date, int, int)
     * @see Calendar#WEEK_OF_YEAR
     * @since 1.0
     */
    public static final Date addWeek(Date date, int week) {
        return operateDate(date, Calendar.WEEK_OF_YEAR, week);
    }

    /**
     * ?? {@link Calendar#HOUR_OF_DAY} 24?(HOUR?,??)<br>
     * ,.
     * 
     * <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>
     * 
     * @param date
     *            the date
     * @param hour
     *            the hour
     * @return the date
     * @see #operateDate(Date, int, int)
     * @see Calendar#HOUR_OF_DAY
     */
    public static final Date addHour(Date date, int hour) {
        // Calendar.HOUR12??
        // Calendar.HOUR_OF_DAY24??
        return operateDate(date, Calendar.HOUR_OF_DAY, hour);
    }

    /**
     * ? Calendar.MINUTE(MINUTE?,??)<br>
     * ,.
     * 
     * <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
     * @return the date
     * @see #operateDate(Date, int, int)
     * @see Calendar#MINUTE
     */
    public static final Date addMinute(Date date, int minute) {
        return operateDate(date, Calendar.MINUTE, minute);
    }

    /**
     * ? Calendar.SECOND(SECOND?,??)<br>
     * ,.
     * 
     * <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
     *            ?
     * @return the date
     * @see #operateDate(Date, int, int)
     * @see Calendar#SECOND
     */
    public static final Date addSecond(Date date, int second) {
        return operateDate(date, Calendar.SECOND, second);
    }

    /**
     * ? <br>
     * ???.
     * 
     * @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)
     * @since 1.0
     */
    public static final Date operateDate(Date currentDate, int field, int amount) {
        Calendar calendar = toCalendar(currentDate);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    // [end]

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

    /**
     * ?(?+1?).
     * 
     * <pre>
     * 2012-06-29
     * return 6
     * </pre>
     * 
     * @param date
     *            the date
     * @return ?
     * @see CalendarUtil#getCalendarFieldValue(Date, int)
     * @see Calendar#MONTH
     * @since 1.0
     */
    public static final int getMonth(Date date) {
        return CalendarUtil.getCalendarFieldValue(date, Calendar.MONTH) + 1;
    }

    /**
     * ?, 1,52(365/7=52.14)<br>
     * {@link Calendar#WEEK_OF_YEAR}
     * <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
     * 
     * <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#getCalendarFieldValue(Date, int)
     * @see Calendar#WEEK_OF_YEAR
     * @see Calendar#getFirstDayOfWeek()
     * @see Calendar#getMinimalDaysInFirstWeek()
     * @see Calendar#setMinimalDaysInFirstWeek(int)
     * @since 1.0.7
     */
    public static final int getWeekOfYear(Date date) {
        //      Calendar calendar = DateUtil.toCalendar(date);
        //      calendar.setMinimalDaysInFirstWeek(7);
        //      return calendar.get(Calendar.WEEK_OF_YEAR);
        return CalendarUtil.getCalendarFieldValue(date, Calendar.WEEK_OF_YEAR);
    }

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

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

    /**
     * ?<br>
     * ,1.
     * 
     * <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#getCalendarFieldValue(Date, int)
     * @see Calendar#DAY_OF_WEEK
     */
    public static final int getDayOfWeek(Date date) {
        return CalendarUtil.getCalendarFieldValue(date, Calendar.DAY_OF_WEEK);
    }

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

    /**
     * date  ?<br>
     * max value: 8784.
     * 
     * <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 final int getHourOfYear(Date date) {
        Date firstDateOfThisYear = getFirstDateOfThisYear(date);
        return getIntervalHour(firstDateOfThisYear, date);
    }

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

    /**
     * .
     * 
     * <pre>
     * 2012-6-29 00:26:53
     * return 53
     * </pre>
     * 
     * @param date
     *            date
     * @return 
     * @see CalendarUtil#getCalendarFieldValue(Date, int)
     * @see Calendar#SECOND
     * @since 1.0
     */
    public static final int getSecond(Date date) {
        return CalendarUtil.getCalendarFieldValue(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 final 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 final 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()
     * @since 1.0
     */
    public static final 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)
     * @since 1.0
     */
    public static final 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)
     * @since 1.0
     */
    public static final Date string2Date(String dateString, String datePattern) {
        return DateFormatUtil.parse(dateString, datePattern);
    }

    // [end]

    // [start]interval

    /**
     * ?.
     * 
     * @param spaceMillisecond
     *            
     * @return ?
     * @see TimeInterval#SECONDS_PER_HOUR
     * @since 1.0
     */
    public static final int getIntervalHour(long spaceMillisecond) {
        // ?
        return (int) (spaceMillisecond / (TimeInterval.SECONDS_PER_HOUR * 1000));
    }

    /**
     * .
     * 
     * @param spaceMillisecond
     *            
     * @return 
     * @see TimeInterval#SECONDS_PER_MINUTE
     * @since 1.0
     */
    public static final int getIntervalMinute(long spaceMillisecond) {
        // 
        return (int) (spaceMillisecond / (TimeInterval.SECONDS_PER_MINUTE * 1000));
    }

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

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

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

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

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

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

    /**
     * ,?date1?date2,??.
     * 
     * @param date1
     *            date1
     * @param date2
     *            date2
     * @return ,?date1?date2,??
     * @see Math#abs(long)
     * @see #getTime(Date)
     * @since 1.0
     */
    public static final long getIntervalTime(Date date1, Date date2) {
        return Math.abs(getTime(date2) - getTime(date1));
    }

    // [end]

    // [start]toCalendar

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

    // [end]

    // [start]isBefore 

    /**
     * ??,?Date, date_before ?date_after<br>
     * :
     * 
     * <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 final 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 final boolean isBefore(Date before, String dateAfter, String datePattern) {
        Date after = string2Date(dateAfter, datePattern);
        return before.before(after);
    }

    // [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 final 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 final boolean isInTime(Date date, Date beginTimeDate, Date endTimeDate) {
        boolean flag = date.after(beginTimeDate) && date.before(endTimeDate);
        return flag;
    }

    // [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 final boolean isEquals(Date date1, Date date2, String datePattern) {
        return date2String(date1, datePattern).equals(date2String(date2, datePattern));
    }

    // [end]

    // [start]isLeapYear 

    /**
     * ??
     * 
     * <h3>:</h3>
     * 
     * <pre>
     * ??3655?4846?365.24219?tropical year.
     *   
     *  ?3650.242221366.
     *         
     *  ?40097.????0.0078?3?
     *  ???.
     *  
     *  400???400?,100?,
     *  ??.
     *  
     *  20001900.
     * </pre>
     * 
     * @param year
     *            
     * @return ??
     * @see GregorianCalendar#isLeapYear(int)
     * @since 1.0
     */
    public static final boolean isLeapYear(int year) {
        // GregorianCalendar calendar = new GregorianCalendar();
        // calendar.isLeapYear(year);

        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    // [end]

    // [start]private

    /**
     * ,<code>00:00:00.000</code>
     * 
     * @param calendar
     *            the calendar
     * @see Calendar#set(int, int)
     * @see Calendar#HOUR_OF_DAY
     * @see Calendar#MINUTE
     * @see Calendar#SECOND
     * @see Calendar#MILLISECOND
     */
    private static void dayBegin(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    /**
     * ?,? <code>23:59:59.999</code>
     * 
     * @param calendar
     *            the calendar
     * @see Calendar#set(int, int)
     * @see Calendar#HOUR_OF_DAY
     * @see Calendar#MINUTE
     * @see Calendar#SECOND
     * @see Calendar#MILLISECOND
     */
    private static void dayEnd(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
    }
    // [end]
}