Java tutorial
/* * 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.feilong.core.date; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateUtils; import com.feilong.core.DatePattern; import com.feilong.core.TimeInterval; import com.feilong.tools.slf4j.Slf4jUtil; import static com.feilong.core.TimeInterval.SECONDS_PER_HOUR; import static com.feilong.core.TimeInterval.SECONDS_PER_MINUTE; /** * {@link java.util.Date}?(feilong-core). * * <h3>:</h3> * * <blockquote> * <table border="1" cellspacing="0" cellpadding="4" summary=""> * <tr style="background-color:#ccccff"> * <th align="left"></th> * <th align="left"></th> * </tr> * <tr valign="top"> * <td></td> * <td> * <ul> * <li>{@link DateUtil#toDate(String, String...)}</li> * </ul> * </td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td></td> * <td> * <ul> * <li>{@link DateUtil#toString(Date, String)}</li> * </ul> * </td> * </tr> * <tr valign="top"> * <td>?</td> * <td> * <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> * </td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>?</td> * <td> * <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> * </td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td></td> * <td> * <ul> * <li>{@link DateUtil#isLeapYear(int)}</li> * </ul> * </td> * </tr> * <tr valign="top"> * <td></td> * <td> * <ul> * <li>{@link DateUtil#isEquals(Date, Date, String)}</li> * </ul> * </td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td></td> * <td> * <ul> * <li>{@link DateUtil#isBefore(Date, Date)}</li> * <li>{@link DateUtil#isBefore(Date, Date)}</li> * </ul> * </td> * </tr> * <tr valign="top"> * <td></td> * <td> * <ul> * <li>{@link DateUtil#isInTime(Date, Date, Date)}</li> * </ul> * </td> * </tr> * </table> * </blockquote> * * <h3>,??:</h3> * * <blockquote> * <table border="1" cellspacing="0" cellpadding="4" summary=""> * <tr style="background-color:#ccccff"> * <th align="left"></th> * <th align="left"></th> * </tr> * <tr valign="top"> * <td></td> * <td>DateUtil.<b>getFirstDateOfThisWeek</b>(DateUtil.addDay(date, 7));</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>?</td> * <td>DateUtil.<b>getLastDateOfThisWeek</b>(DateUtil.addDay(date, 7));</td> * </tr> * <tr valign="top"> * <td></td> * <td>DateUtil.<b>getFirstDateOfThisWeek</b>(DateUtil.addDay(date, -7));</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>?</td> * <td>DateUtil.<b>getLastDateOfThisWeek</b>(DateUtil.addDay(date, -7));</td> * </tr> * <tr valign="top"> * <td></td> * <td>DateUtil.<b>getFirstDateOfThisMonth</b>(DateUtil.addMonth(now, +1)));</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>?</td> * <td>DateUtil.<b>getLastDateOfThisMonth</b>(DateUtil.addMonth(now, +1)));</td> * </tr> * <tr valign="top"> * <td></td> * <td>DateUtil.<b>getFirstDateOfThisMonth</b>(DateUtil.addMonth(now, -1)));</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>?</td> * <td>DateUtil.<b>getLastDateOfThisMonth</b>(DateUtil.addMonth(now, -1)));</td> * </tr> * <tr valign="top"> * <td></td> * <td>DateUtil.<b>getFirstDateOfThisYear</b>(DateUtil.addYear(now, -1));</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>?</td> * <td>DateUtil.<b>getLastDateOfThisYear</b>(DateUtil.addYear(now, -1)));</td> * </tr> * <tr valign="top"> * <td></td> * <td>DateUtil.<b>getFirstDateOfThisYear</b>(DateUtil.addYear(now, +1));</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>?</td> * <td>DateUtil.<b>getLastDateOfThisYear</b>(DateUtil.addYear(now, +1)));</td> * </tr> * </table> * </blockquote> * * @author <a href="http://feitianbenyue.iteye.com/">feilong</a> * @see CalendarUtil * @see DatePattern * @see org.apache.commons.lang3.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!"); } /** * <code>date</code>, <code>00:00:00</code>. * * <pre class="code"> * DateUtil.getFirstDateOfThisDay(2011-01-01 10:20:20) =2011-01-01 00:00:00 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException} * @see org.apache.commons.lang3.time.DateUtils#truncate(Date, int) * @since 1.5.0 */ public static Date getFirstDateOfThisDay(Date date) { Calendar calendar = toCalendar(date); return CalendarUtil.toDate(CalendarUtil.resetDayBegin(calendar)); } /** * <code>date</code>?,<code>23:59:59.999</code>. * * <pre class="code"> * DateUtil.getLastDateOfThisDay(2011-01-01 10:20:20)=2011-01-01 23:59:59.999 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException} * @since 1.5.0 */ public static Date getLastDateOfThisDay(Date date) { Calendar calendar = toCalendar(date); return CalendarUtil.toDate(CalendarUtil.resetDayEnd(calendar)); } // *****************************week**************************************************** /** * date <span style="color:red"> ()</span> <code>00:00:00.000</code> . * * <pre class="code"> * DateUtil.getFirstDateOfThisWeek(2012-10-11 17:10:30.701) =2012-10-07 00:00:00.000 * DateUtil.getFirstDateOfThisWeek(2014-01-01 05:00:00) =2013-12-29 00:00:00.000 // * </pre> * * <h3>?:</h3> * <blockquote> * <ol> * <li>,<span style="color:red">,?</span></li> * <li>,?</li> * </ol> * </blockquote> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException} * @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); return CalendarUtil.toDate(CalendarUtil.resetDayBegin(calendar)); } /** * date <span style="color:red">?()</span> <code>23:59:59.999</code> . * * <pre class="code"> * DateUtil.getLastDateOfThisWeek(2012-10-11 17:10:30.701) =2012-10-13 23:59:59.999 * DateUtil.getLastDateOfThisWeek(2014-12-31 05:00:00) =2015-01-03 23:59:59.999 // * </pre> * * <h3>?:</h3> * <blockquote> * <ol> * <li>,<span style="color:red">,?</span></li> * <li>,?</li> * </ol> * </blockquote> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException} * @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); return CalendarUtil.toDate(CalendarUtil.resetDayEnd(calendar)); } // ********************************************************************************* /** * <code>date</code>,<code>00:00:00.000</code>. * * <pre class="code"> * DateUtil.getFirstDateOfThisMonth(2012-10-11 17:10:30.701)=2012-10-01 00:00:00.000 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException} * @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); return CalendarUtil.toDate(CalendarUtil.resetDayBegin(calendar)); } /** * <code>date</code><span style="color:red">?</span>,<code>23:59:59.999</code> . * * <p> * <code>date</code> ,,2 2829 * </p> * * <pre class="code"> * DateUtil.getLastDateOfThisMonth(2012-10-11 17:10:30.701)=2012-10-31 23:59:59.999 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException} * @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)); return CalendarUtil.toDate(CalendarUtil.resetDayEnd(calendar)); } /** * <code>date</code><span style="color:red"></span>,<code>00:00:00.000</code> . * * <pre class="code"> * DateUtil.getFirstDateOfThisYear(2012-10-11 17:10:30.701)=2012-01-01 00:00:00 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException} * @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); return CalendarUtil.toDate(CalendarUtil.resetDayBegin(calendar)); } /** * <code>date</code> <code>week</code>. * * <pre class="code"> * 2015-7-29 14:08 * DateUtil.getFirstWeekOfSpecifyDateYear(NOW, Calendar.FRIDAY) =2015-01-02 00:00:00.000 * DateUtil.getFirstWeekOfSpecifyDateYear(NOW, Calendar.MONDAY) =2015-01-05 00:00:00.000 * </pre> * * <p> * {@link Calendar#DAY_OF_WEEK_IN_MONTH} ?. DAY_OF_WEEK ,???.<br> * {@link Calendar#WEEK_OF_MONTH} {@link Calendar#WEEK_OF_YEAR} ??,?? {@link Calendar#getFirstDayOfWeek()} * {@link Calendar#getMinimalDaysInFirstWeek()}. * </p> * * <p> * DAY_OF_MONTH 1 7 DAY_OF_WEEK_IN_MONTH 1;<br> * 8 14 DAY_OF_WEEK_IN_MONTH 2,?.<br> * DAY_OF_WEEK_IN_MONTH 0 DAY_OF_WEEK_IN_MONTH 1 ?.<br> * ?,,? DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1.<br> * ?,????.<br> * , 31 , DAY_OF_WEEK_IN_MONTH -1 DAY_OF_WEEK_IN_MONTH 5 DAY_OF_WEEK_IN_MONTH 4 ?? * </p> * * @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 <code>date</code> null, {@link NullPointerException} * @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); return CalendarUtil.toDate(calendar); } /** * <code>date</code><span style="color:red">?</span> <code>23:59:59.999</code> . * * <pre class="code"> * DateUtil.getLastDateOfThisYear(2012-10-11 17:10:30.701)=2012-12-31 23:59:59.999 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException} * @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); return CalendarUtil.toDate(CalendarUtil.resetYearEnd(calendar)); } // [start]operate ?(?) /** * <code>date</code>,?( {@link java.util.Calendar#YEAR}?,??),,. * * <p> * ?<code>date</code>?? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.addYear(2012-06-29 00:33:05,5) =20<span style="color:red">17</span>-06-29 00:33:05 * DateUtil.addYear(2012-06-29 00:33:05,-5) =20<span style="color:red">07</span>-06-29 00:33:05 * </pre> * * </blockquote> * * @param date * ? * @param year * ? ?? * @return <code>date</code>null, {@link java.lang.IllegalArgumentException} * @see Calendar#YEAR * @see org.apache.commons.lang3.time.DateUtils#addYears(Date, int) */ public static Date addYear(Date date, int year) { return DateUtils.addYears(date, year); } /** * <code>date</code>?,( {@link java.util.Calendar#MONTH}?,??),,. * * <p> * ?<code>date</code>?? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.addMonth(2012-10-16 23:12:43,5) =2013-03-16 23:12:43.932 * DateUtil.addMonth(2012-10-16 23:12:43,-5) =2012-05-16 23:12:43.943 * </pre> * * </blockquote> * * @param date * ? * @param month * ?, <span style="color:red">?</span>,??<br> * -3 3? * @return <code>date</code>null, {@link java.lang.IllegalArgumentException} * @see Calendar#MONTH * @see org.apache.commons.lang3.time.DateUtils#addMonths(Date, int) */ public static Date addMonth(Date date, int month) { return DateUtils.addMonths(date, month); } /** * <code>date</code>? ( {@link java.util.Calendar#DAY_OF_MONTH}?,??),,. * * <p> * ?<code>date</code>?? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.addDay(2012-06-29 00:42:26,5) =2012-07-04 00:42:26 * DateUtil.addDay(2012-06-29 00:42:26,-5) =2012-06-24 00:42:26 * DateUtil.addDay(2014-12-31 02:10:05,5) =2015-01-05 02:10:05.000 * DateUtil.addDay(2014-01-01 02:10:05,-5) =2013-12-27 02:10:05.000 * </pre> * * </blockquote> * * @param date * ? * @param day * ??,<span style="color:red">?</span>,??<br> * @return <code>date</code>null, {@link java.lang.IllegalArgumentException} * @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 DateUtils.addDays(date, day); } /** * <code>date</code>? ({@link Calendar#WEEK_OF_YEAR}?,??),,. * <p> * ?<code>date</code>?? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.addWeek(2012-06-29 00:45:18,5) =2012-08-03 00:45:18 * DateUtil.addWeek(2012-06-29 00:45:18,-5) =2012-05-25 00:45:18 * </pre> * * </blockquote> * * @param date * ? * @param week * ??,<span style="color:red">?</span>,??<br> * @return <code>date</code>null, {@link java.lang.IllegalArgumentException} * @see org.apache.commons.lang3.time.DateUtils#addWeeks(Date, int) */ public static Date addWeek(Date date, int week) { return DateUtils.addWeeks(date, week); } /** * <code>date</code>?? ({@link Calendar#HOUR_OF_DAY} 24??,??),,. * * <p> * ?<code>date</code>?? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.addHour(2012-06-29 00:46:24,5) =2012-06-29 05:46:24 * DateUtil.addHour(2012-06-29 00:46:24,-5) =2012-06-28 19:46:24 * </pre> * * </blockquote> * <p> * {@link Calendar#HOUR}:12?? <br> * {@link Calendar#HOUR_OF_DAY}:24?? * </p> * * @param date * ? * @param hour * the hour,<span style="color:red">?</span>,??<br> * @return <code>date</code>null, {@link java.lang.IllegalArgumentException} * @see org.apache.commons.lang3.time.DateUtils#addHours(Date, int) */ public static Date addHour(Date date, int hour) { return DateUtils.addHours(date, hour); } /** * <code>date</code>? ({@link Calendar#MINUTE}?,??),,. * * <p> * ?<code>date</code>?? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.addMinute(2012-10-16 23:20:33,180) =2012-10-17 02:20:33.669 * DateUtil.addMinute(2012-10-16 23:20:33,-180) =2012-10-16 20:20:33.669 * </pre> * * </blockquote> * * @param date * ? * @param minute * the minute,<span style="color:red">?</span>,??<br> * @return <code>date</code>null, {@link java.lang.IllegalArgumentException} * @see org.apache.commons.lang3.time.DateUtils#addMinutes(Date, int) */ public static Date addMinute(Date date, int minute) { return DateUtils.addMinutes(date, minute); } /** * <code>date</code>? ({@link java.util.Calendar#SECOND}?,??),,. * * <p> * ?<code>date</code>?? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.addSecond(2012-10-16 23:22:02,180) =2012-10-16 23:25:02.206 * DateUtil.addSecond(2012-10-16 23:22:02,-180) =2012-10-16 23:19:02.206 * </pre> * * </blockquote> * * @param date * ? * @param second * ?,<span style="color:red">?</span>,??<br> * @return <code>date</code>null, {@link java.lang.IllegalArgumentException} * @see org.apache.commons.lang3.time.DateUtils#addSeconds(Date, int) */ public static Date addSecond(Date date, int second) { return DateUtils.addSeconds(date, second); } /** * <code>date</code>? ({@link java.util.Calendar#MILLISECOND}?,??),,. * * <p> * ?<code>date</code>?? * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.addMillisecond(2015-09-07 13:35:02.769,5000) =2015-09-07 13:35:07.769 * DateUtil.addMillisecond(2015-09-07 13:35:02.769,-5000) =2015-09-07 13:34:57.769 * </pre> * * </blockquote> * * @param date * ? * @param millisecond * ?,<span style="color:red">?</span>,??<br> * @return <code>date</code>null, {@link java.lang.IllegalArgumentException} * @see org.apache.commons.lang3.time.DateUtils#addMilliseconds(Date, int) * @since 1.4.1 */ public static Date addMillisecond(Date date, int millisecond) { return DateUtils.addMilliseconds(date, millisecond); } // [end] // [start]fieldValue? /** * ? <code>date</code> {@link java.util.Calendar#YEAR}. * * <pre class="code"> * DateUtil.getYear(toDate("2012-06-29 00:26:53", COMMON_DATE_AND_TIME)) = 2012 * DateUtil.getYear(toDate("2016-07-16", COMMON_DATE)) = 2016 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @see CalendarUtil#getFieldValue(Date, int) * @see Calendar#YEAR */ public static int getYear(Date date) { return CalendarUtil.getFieldValue(date, Calendar.YEAR); } /** * ? <code>date</code>{@link java.util.Calendar#MONTH}<span style="color:red">(?+1?)</span>. * * <pre class="code"> * DateUtil.getMonth(<code>2012-06-29</code>) =6 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @see CalendarUtil#getFieldValue(Date, int) * @see Calendar#MONTH */ public static int getMonth(Date date) { return 1 + CalendarUtil.getFieldValue(date, Calendar.MONTH); } /** * <code>date</code>{@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 class="code"> * DateUtil.getWeekOfYear(2014-06-03) =23 * DateUtil.getWeekOfYear(2014-01-01) =1 * DateUtil.getWeekOfYear(2014-12-29) =23 * DateUtil.getWeekOfYear(2014-12-20) =51 * DateUtil.getWeekOfYear(2014-12-26) =52 * </pre> * * {@link Calendar#setMinimalDaysInFirstWeek(int)} ???,7? * * <pre class="code"> * DateUtil.getWeekOfYear(2014-01-01) =52 * DateUtil.getWeekOfYear(2014-12-31) =52 * </pre> * * ?,11??,?7,11? * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @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); } /** * ? <code>date</code>(). * * <pre class="code"> * DateUtil.getDayOfYear(<code>2013-01-01</code>) =1 * DateUtil.getDayOfYear(<code>2013-01-05</code>) =5 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @since 1.0.2 */ public static int getDayOfYear(Date date) { return CalendarUtil.getFieldValue(date, Calendar.DAY_OF_YEAR); } /** * ? <code>date</code>{@link Calendar#DAY_OF_MONTH}. * * <pre class="code"> * DateUtil.getDayOfMonth(<code>2012-06-29</code>) =29 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @see CalendarUtil#getFieldValue(Date, int) * @see Calendar#DAY_OF_MONTH */ public static int getDayOfMonth(Date date) { return CalendarUtil.getFieldValue(date, Calendar.DAY_OF_MONTH); } /** * <code>date</code>{@link Calendar#DAY_OF_WEEK}. * * <p style="color:red"> * ,1. SUNDAY?MONDAY?TUESDAY?WEDNESDAY?THURSDAY?FRIDAY SATURDAY ,1-7 * </p> * * <pre class="code"> * DateUtil.getDayOfWeek(2012-6-29) =6 5 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @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); } /** * <code>date</code>?(24?){@link Calendar#HOUR_OF_DAY}. * * <pre class="code"> * DateUtil.getHourOfDay(toDate("2012-06-29 00:26:53", COMMON_DATE_AND_TIME)) =0 * DateUtil.getHourOfDay(toDate("2016-07-16 22:34:00", COMMON_DATE_AND_TIME)) =22 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @see CalendarUtil#getFieldValue(Date, int) * @see Calendar#HOUR_OF_DAY */ public static int getHourOfDay(Date date) { return CalendarUtil.getFieldValue(date, Calendar.HOUR_OF_DAY); } /** * <code>date</code> ?. * * <p> * max value: 8784. * </p> * * <pre class="code"> * DateUtil.getHourOfYear(2013-01-01 00:00:05) =0 * DateUtil.getHourOfYear(2013-01-01 01:00:05) =1 * DateUtil.getHourOfYear(2013-01-05 12:00:05) =108 * DateUtil.getHourOfYear(2013-09-09 17:28) =6041 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @since 1.0.2 */ public static int getHourOfYear(Date date) { return (getDayOfYear(date) - 1) * 24 + CalendarUtil.getFieldValue(date, Calendar.HOUR_OF_DAY); } /** * <code>date</code> {@link java.util.Calendar#MINUTE}. * * <pre class="code"> * DateUtil.getMinute(2012-6-29 00:26:53) =26 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @see CalendarUtil#getFieldValue(Date, int) * @see Calendar#MINUTE */ public static int getMinute(Date date) { return CalendarUtil.getFieldValue(date, Calendar.MINUTE); } /** * <code>date</code>{@link java.util.Calendar#SECOND}. * * <pre class="code"> * DateUtil.getSecond(2012-6-29 00:26:53) =53 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @see CalendarUtil#getFieldValue(Date, int) * @see Calendar#SECOND */ public static int getSecond(Date date) { return CalendarUtil.getFieldValue(date, Calendar.SECOND); } /** * <code>date</code>,86400 {@link TimeInterval#SECONDS_PER_DAY}. * * <pre class="code"> * DateUtil.getSecondOfDay(2013-09-09 16:42:41)= 60161 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @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 * SECONDS_PER_HOUR + getSecondOfHour(date); } /** * <code>date</code>??,3600 {@link TimeInterval#SECONDS_PER_HOUR}. * * <pre class="code"> * DateUtil.getSecondOfHour(2013-09-15 01:15:23)= 923 * </pre> * * @param date * ? * @return <code>date</code> null, {@link NullPointerException}<br> * @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 * SECONDS_PER_MINUTE; } /** * <code>1970 1 1 00:00:00 GMT</code> ?, Date . * * <pre class="code"> * DateUtil.getTime(2012-6-29 00:28)= 1340900883288 * </pre> * * @param date * ? * @return {@link Date#getTime()} <br> * <code>date</code> null, {@link NullPointerException}<br> */ public static long getTime(Date date) { Validate.notNull(date, "date can't be null!"); return date.getTime(); } // [end] // [start]toString/toDate ? /** * <code>date</code>???. * * <pre class="code"> * DateUtil.toString(Tue Oct 16 23:49:21 CST 2012,DatePattern.COMMON_DATE_AND_TIME_WITH_MILLISECOND) =2012-10-16 23:49:21.525 * </pre> * * @param date * ? * @param datePattern * ? {@link DatePattern} * @return <code>date</code> null, {@link NullPointerException}<br> * <code>pattern</code> null, {@link NullPointerException}<br> * <code>pattern</code> blank, {@link IllegalArgumentException}<br> * ? {@link java.util.Locale#getDefault()},{@link java.text.DateFormat#format(Date)} * @see org.apache.commons.lang3.time.DateFormatUtils#format(Date, String) * @see "org.joda.time.base.AbstractDateTime#toString(String)" * @see <a href="http://stackoverflow.com/questions/5683728/convert-java-util-date-to-string">convert-java-util-date-to-string</a> * @see <a href="http://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string">change-date-format-in-a-java-string</a> * @since 1.6.0 */ public static String toString(Date date, String datePattern) { return DateFormatUtils.format(date, datePattern); } /** * <code>dateString</code> ?? <code>datePattern</code> ????date. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateUtil.toDate("2016-02-33", DatePattern.COMMON_DATE) = 2016-03-04 * DateUtil.toDate("2016-06-28T01:21:12-0800", "yyyy-MM-dd'T'HH:mm:ssZ") = 2016-06-28 17:21:12 * DateUtil.toDate("2016-06-28T01:21:12+0800", "yyyy-MM-dd'T'HH:mm:ssZ") = 2016-06-28 01:21:12 * </pre> * * </blockquote> * * <h3>?:</h3> * <blockquote> * <ol> * <li>?,<b>??</b>,?? {@link java.text.DateFormat#setLenient(boolean)},??"2016-02-33",?? 2016-03-04</li> * <li>?,?</li> * <li>??,</li> * </ol> * </blockquote> * * @param dateString * * @param parsePatterns * ?,?{@link DatePattern} * @return <code>dateString</code> null, {@link NullPointerException}<br> * <code>dateString</code> blank, {@link IllegalArgumentException}<br> * <code>parsePatterns</code> null, {@link NullPointerException}<br> * @see org.apache.commons.lang3.time.DateUtils#parseDate(String, String...) * @see <a href="http://stackoverflow.com/questions/4216745/java-string-to-date-conversion/">java-string-to-date-conversion</a> * @see <a href="http://stackoverflow.com/questions/4216745/java-string-to-date-conversion/22180505#22180505">java-string-to-date- * conversion/22180505#22180505</a> * @see <a href="http://stackoverflow.com/questions/2735023/convert-string-to-java-util-date">convert-string-to-java-util-date</a> * @since 1.7.3 change param to parsePatterns array */ public static Date toDate(String dateString, String... parsePatterns) { Validate.notBlank(dateString, "dateString can't be blank!"); Validate.notNull(parsePatterns, "parsePatterns can't be null!"); try { return DateUtils.parseDate(dateString, parsePatterns); } catch (ParseException e) { String pattern = "parse dateString [{}] use patterns:[{}] to date exception,message:[{}]"; throw new IllegalArgumentException(Slf4jUtil.format(pattern, dateString, parsePatterns, e.getMessage()), e); } } // [end] // [start]toCalendar /** * {@link Date} ? {@link 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 * ? * @return date null, {@link NullPointerException} * @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) { Validate.notNull(date, "date can't be null!"); return DateUtils.toCalendar(date); } // [end] // [start]isBefore /** * <code>date</code>? <code>whenDate</code>?. * * @param date * * @param whenDate * * @return <code>date</code> null,false<br> * <code>whenDate</code> null,<br> * ? <code>date.before(when)</code> * @see java.util.Date#before(Date) * @since 1.2.2 */ public static boolean isBefore(Date date, Date whenDate) { Validate.notNull(whenDate, "whenDate can't be null!"); return null == date ? false : date.before(whenDate); } /** * <code>date</code>? <code>whenDate</code>?. * * @param date * * @param whenDate * * @return <code>date</code> null,false<br> * <code>when</code> null,<br> * ? <code>date.after(when)</code> * @see java.util.Date#after(Date) * @since 1.2.2 */ public static boolean isAfter(Date date, Date whenDate) { Validate.notNull(whenDate, "whenDate can't be null!"); return null == date ? false : date.after(whenDate); } // [end] // [start]isInTime /** * <code>date</code> ? <code>beginTimeDate</code> <code>endTimeDate</code>. * * <pre class="code"> * DateUtil.isInTime("2012-10-16 23:00:02", "2012-10-10 22:59:00", "2012-10-18 22:59:00") = true * </pre> * * @param date * * @param beginTimeDate * the begin time date * @param endTimeDate * the end time date * @return <code>date</code> <code>beginTimeDate</code>?, <code>date</code> <code>endTimeDate</code>?,true<br> * @throws NullPointerException * <code>date</code> null, <code>beginTimeDate</code> null <code>endTimeDate</code> null * @see Date#after(Date) * @see Date#before(Date) */ public static boolean isInTime(Date date, Date beginTimeDate, Date endTimeDate) { Validate.notNull(date, "date can't be null!"); Validate.notNull(beginTimeDate, "beginTimeDate can't be null!"); Validate.notNull(endTimeDate, "endTimeDate can't be null!"); return date.after(beginTimeDate) && date.before(endTimeDate); } // [end] // [start]isEquals /** * ?? <code>datePattern</code>,??. * * @param date1 * 1 * @param date2 * 2 * @param datePattern * ? {@link DatePattern} * @return true,?false * @see #toString(Date, String) * @see org.apache.commons.lang3.time.DateUtils#isSameDay(Date, Date) * @since 1.0.5 change name from isEqual to isEquals */ public static boolean isEquals(Date date1, Date date2, String datePattern) { return toString(date1, datePattern).equals(toString(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> * ?365,0.2422,,21,366,.<br> * </p> * * <p> * ?40097.?,???0.0078,?3?,<br> * ,???. * </p> * * <p> * ,,400??,?400?,100?,,<br> * :,?,?.<br> * * ,2000,1900.<br> * </p> * </blockquote> * * @param year * * @return ,?,? * @see GregorianCalendar#isLeapYear(int) */ public static boolean isLeapYear(int year) { return new GregorianCalendar().isLeapYear(year); } // [end] }