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.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.apache.commons.lang3.Validate; import com.feilong.core.TimeInterval; import static com.feilong.core.TimeInterval.MILLISECOND_PER_DAY; import static com.feilong.core.TimeInterval.MILLISECOND_PER_HOUR; import static com.feilong.core.TimeInterval.MILLISECOND_PER_MINUTE; import static com.feilong.core.TimeInterval.MILLISECOND_PER_WEEK; import static com.feilong.core.bean.ConvertUtil.toArray; /** * . * * <h3> {@link DateUtil} :</h3> * * <blockquote> * <p> * {@link DateUtil}?Date API, {@link DateExtensionUtil},. * </p> * </blockquote> * * <h3>:</h3> * * <blockquote> * <ul> * <li>{@link #getIntervalDayList(Date, Date)}</li> * <li>{@link #getIntervalForView(long)}</li> * <li>{@link #getIntervalForView(Date, Date)}</li> * </ul> * </blockquote> * * <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 #getIntervalDay(long)}</li> * <li>{@link #getIntervalDay(Date, Date)}</li> * * <li>{@link #getIntervalWeek(long)}</li> * <li>{@link #getIntervalWeek(Date, Date)}</li> * * <li>{@link #getIntervalHour(long)}</li> * <li>{@link #getIntervalHour(Date, Date)}</li> * * <li>{@link #getIntervalMinute(long)}</li> * <li>{@link #getIntervalSecond(long)}</li> * <li>{@link #getIntervalSecond(Date, Date)}</li> * * <li>{@link #getIntervalTime(Date, Date)}</li> * </ul> * </td> * </tr> * * </table> * </blockquote> * * @author <a href="http://feitianbenyue.iteye.com/">feilong</a> * @since 1.0.8 */ public final class DateExtensionUtil { /** . */ private static final String DAY = ""; /** ?. */ private static final String HOUR = "?"; /** . */ private static final String MINUTE = ""; /** . */ private static final String SECOND = ""; /** . */ private static final String MILLISECOND = ""; /** Don't let anyone instantiate this class. */ private DateExtensionUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } // [start] /,?sql /** * ?, <code>00:00:00</code> ? <code>00:00:00</code>. * * <p> * ?,between ... and ... * </p> * * <pre class="code"> * 2012-10-16 22:18:34 * * <b>:</b> * { * 2012-10-16 00:00:00.000, * 2012-10-17 00:00:00.000 * } * </pre> * * @return Date today tomorrow */ public static Date[] getResetTodayAndTomorrow() { Date today = DateUtil.getFirstDateOfThisDay(new Date()); return toArray(today, DateUtil.addDay(today, 1)); } /** * ? [yestoday,today]. * * <p> * 00:00 <br> * 00:00 <br> * sql/hql?,between ... and ... * </p> * * <pre class="code"> * :2012-10-16 22:46:42 * * {2012-10-15 00:00:00.000,2012-10-16 00:00:00.000} * </pre> * * @return Date <br> * 00:00 <br> * 00:00 */ public static Date[] getResetYesterdayAndToday() { Date today = DateUtil.getFirstDateOfThisDay(new Date()); return toArray(DateUtil.addDay(today, -1), today); } // [end] /** * ?(??),. * * <h3>:</h3> * <blockquote> * * <pre class="code"> * Date fromDate = DateUtil.toDate("2011-03-5 23:31:25.456", DatePattern.COMMON_DATE_AND_TIME); * Date toDate = DateUtil.toDate("2011-03-10 01:30:24.895", DatePattern.COMMON_DATE_AND_TIME); * LOGGER.debug(JsonUtil.format(DateExtensionUtil.getIntervalDayList(fromDate, toDate))); * </pre> * * <b>:</b> * * <pre class="code"> * ["2011-03-05 00:00:00", * "2011-03-06 00:00:00", * "2011-03-07 00:00:00", * "2011-03-08 00:00:00", * "2011-03-09 00:00:00", * "2011-03-10 00:00:00" * ] * </pre> * * </blockquote> * * <h3>:</h3> * <blockquote> * <ol> * <li>?? <code>00:00:00.000</code></li> * <li> <code>fromDate</code> <code>toDate</code></li> * </ol> * </blockquote> * * @param fromDate * the from date * @param toDate * the to date * @return <code>fromDate</code> null, {@link NullPointerException}<br> * <code>toDate</code> null, {@link NullPointerException} * @see #getIntervalDay(Date, Date) * @see org.apache.commons.lang3.time.DateUtils#iterator(Calendar, int) * @since 1.5.4 */ public static List<Date> getIntervalDayList(Date fromDate, Date toDate) { Validate.notNull(fromDate, "fromDate can't be null!"); Validate.notNull(toDate, "toDate can't be null!"); Date minDate = fromDate.before(toDate) ? fromDate : toDate; Date maxDate = fromDate.before(toDate) ? toDate : fromDate; // ******?******** Date beginDateReset = DateUtil.getFirstDateOfThisDay(minDate); Date endDateReset = DateUtil.getLastDateOfThisDay(maxDate); List<Date> list = new ArrayList<Date>(); list.add(beginDateReset); // int intervalDay = getIntervalDay(beginDateReset, endDateReset); for (int i = 0; i < intervalDay; ++i) { list.add(DateUtil.addDay(beginDateReset, i + 1)); } return list; } //**************************************************************************************************** /** * <code>beginDate</code> ? <code>new Date()</code>,<span style="color:red">?</span>,???. * * <h3>:</h3> * * <blockquote> * <ol> * <li>?</li> * <li> <code>beginDate</code> ? <code>new Date()</code> ?,?care ??</li> * <li>? ,?,,, </li> * </ol> * </blockquote> * * <h3>:</h3> * <blockquote> * * <pre class="code"> * Date beginDate = new Date(); * * // do some logic * // balabala logic * * LOGGER.info("use time:}{}", DateExtensionUtil.getIntervalForView(beginDate)); * * </pre> * * </blockquote> * * <h3>:</h3> * * <blockquote> * <p> * ? 2016-07-09 13:03:53.259 * </p> * * <pre class="code"> * Date date = toDate("2016-07-03 00:00:00", COMMON_DATE_AND_TIME); * LOGGER.debug(getIntervalForView(date)); * </pre> * * : * * <pre class="code"> * 613?353259 * </pre> * * </blockquote> * * @param beginDate * * @return <code>beginDate</code> null, {@link NullPointerException}<br> * @see #getIntervalForView(Date, Date) * @see org.apache.commons.lang3.time.DurationFormatUtils#formatDurationWords(long, boolean, boolean) * @since 1.8.0 */ public static String getIntervalForView(Date beginDate) { return getIntervalForView(beginDate, new Date()); } /** * <code>beginDate</code> <code>endDate</code> <span style="color:red">?</span>,???. * * <h3>:</h3> * <blockquote> * <ol> * <li>?</li> * <li> <code>beginDate</code> ? <code>endDate</code> ?,?care ??</li> * <li>? ,?,,, </li> * </ol> * </blockquote> * * <h3>:</h3> * <blockquote> * * <pre class="code"> * Date beginDate = new Date(); * * // do some logic * // balabala logic * * LOGGER.info("use time:}{}", DateExtensionUtil.getIntervalForView(beginDate, new Date())); * * </pre> * * </blockquote> * * <h3>:</h3> * <blockquote> * * <pre class="code"> * DateExtensionUtil.getIntervalForView(2011-05-19 8:30:40,2011-05-19 11:30:24) = 2?5944 * DateExtensionUtil.getIntervalForView(2011-05-19 11:31:25.456,2011-05-19 11:30:24.895) = 11 * </pre> * * </blockquote> * * @param beginDate * * @param endDate * ? * @return <code>beginDate</code> null, {@link NullPointerException}<br> * <code>endDate</code> null, {@link NullPointerException} * @see #getIntervalForView(long) * @see #getIntervalTime(Date, Date) * @see org.apache.commons.lang3.time.DurationFormatUtils#formatDurationWords(long, boolean, boolean) */ public static String getIntervalForView(Date beginDate, Date endDate) { return getIntervalForView(getIntervalTime(beginDate, endDate)); } /** * <code>spaceMilliseconds</code> ,???. * * <h3>:</h3> * <blockquote> * <ol> * <li>?</li> * <li>? ,?,,, </li> * </ol> * </blockquote> * * <h3>:</h3> * <blockquote> * * <pre class="code"> * DateExtensionUtil.getIntervalForView(13516) = 13516 * DateExtensionUtil.getIntervalForView(0) = 0 * </pre> * * </blockquote> * * @param spaceMilliseconds * * @return spaceMilliseconds 0 0<br> * {@code spaceMilliseconds < 0}, {@link IllegalArgumentException} * @see #getIntervalDay(long) * @see #getIntervalHour(long) * @see #getIntervalMinute(long) * @see #getIntervalSecond(long) * @see org.apache.commons.lang3.time.DurationFormatUtils#formatDurationWords(long, boolean, boolean) */ public static String getIntervalForView(long spaceMilliseconds) { Validate.isTrue(spaceMilliseconds >= 0, "spaceMilliseconds can't <0"); if (0 == spaceMilliseconds) { return "0"; } // ************************************************************************************** // long spaceDay = getIntervalDay(spaceMilliseconds); // ? ??, long spaceHour = getIntervalHour(spaceMilliseconds) - spaceDay * 24; // ????, long spaceMinute = getIntervalMinute(spaceMilliseconds) - (spaceDay * 24 + spaceHour) * 60; // ???,?, long spaceSecond = getIntervalSecond(spaceMilliseconds) - ((spaceDay * 24 + spaceHour) * 60 + spaceMinute) * 60; // ???,,?, long spaceMillisecond = spaceMilliseconds - (((spaceDay * 24 + spaceHour) * 60 + spaceMinute) * 60 + spaceSecond) * 1000; // ************************************************************************************** StringBuilder sb = new StringBuilder(); if (0 != spaceDay) { sb.append(spaceDay + DAY); } if (0 != spaceHour) { sb.append(spaceHour + HOUR); } if (0 != spaceMinute) { sb.append(spaceMinute + MINUTE); } if (0 != spaceSecond) { sb.append(spaceSecond + SECOND); } if (0 != spaceMillisecond) { sb.append(spaceMillisecond + MILLISECOND); } return sb.toString(); } // [start]interval /** * . * * @param spaceMilliseconds * * @return * @see TimeInterval#MILLISECOND_PER_MINUTE * @since 1.6.0 */ static int getIntervalMinute(long spaceMilliseconds) { return (int) (spaceMilliseconds / (MILLISECOND_PER_MINUTE)); } /** * (<span style="color:red">?</span>). * * @param date1 * the date1 * @param date2 * the date2 * @return <code>date1</code> null, {@link NullPointerException}<br> * <code>date2</code> null, {@link NullPointerException} * @see #getIntervalTime(Date, Date) * @see #getIntervalSecond(long) * @since 1.6.0 */ public static int getIntervalSecond(Date date1, Date date2) { return getIntervalSecond(getIntervalTime(date1, date2)); } /** * . * * @param spaceMilliseconds * * @return * @since 1.6.0 */ static int getIntervalSecond(long spaceMilliseconds) { return (int) (spaceMilliseconds / 1000); } /** * ?(<span style="color:red">?</span>). * * @param date1 * date1 * @param date2 * date2 * @return <code>date1</code> null, {@link NullPointerException}<br> * <code>date2</code> null, {@link NullPointerException} * @see #getIntervalTime(Date, Date) * @see #getIntervalHour(long) * @since 1.6.0 */ public static int getIntervalHour(Date date1, Date date2) { return getIntervalHour(getIntervalTime(date1, date2)); } /** * ?. * * @param spaceMilliseconds * * @return ? * @see TimeInterval#MILLISECOND_PER_HOUR * @since 1.6.0 */ static int getIntervalHour(long spaceMilliseconds) { return (int) (spaceMilliseconds / (MILLISECOND_PER_HOUR)); } /** * (<span style="color:red">?</span>). * * @param date1 * the date1 * @param date2 * the date2 * @return <code>date1</code> null, {@link NullPointerException}<br> * <code>date2</code> null, {@link NullPointerException} * @see #getIntervalWeek(long) * @since 1.6.0 */ public static int getIntervalWeek(Date date1, Date date2) { return getIntervalWeek(getIntervalTime(date1, date2)); } /** * . * * @param spaceTime * the space time * @return the interval week * @see com.feilong.core.TimeInterval#SECONDS_PER_WEEK * @since 1.6.0 */ private static int getIntervalWeek(long spaceTime) { return (int) (spaceTime / (MILLISECOND_PER_WEEK)); } //-******************getIntervalDay*************************************** /** * (<span style="color:red">?</span>). * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateExtensionUtil.getIntervalDay( * DateUtil.toDate("2008-08-24", DatePattern.COMMON_DATE), * DateUtil.toDate("2008-08-27", DatePattern.COMMON_DATE)) * </pre> * * <b>:</b> 3 * * </blockquote> * * @param date1 * date1 * @param date2 * date2 * @return <code>date1</code> null, {@link NullPointerException}<br> * <code>date2</code> null, {@link NullPointerException} * @see #getIntervalTime(Date, Date) * @see #getIntervalDay(long) * @since 1.6.0 */ public static int getIntervalDay(Date date1, Date date2) { return getIntervalDay(getIntervalTime(date1, date2)); } /** * . * * @param spaceMilliseconds * * @return * @see TimeInterval#SECONDS_PER_DAY * @since 1.6.0 */ static int getIntervalDay(long spaceMilliseconds) { return (int) (spaceMilliseconds / (MILLISECOND_PER_DAY)); } /** * <b></b> (<span style="color:red">?</span>). * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * DateExtensionUtil.getIntervalTime( * toDate("2016-07-16 15:21:00", COMMON_DATE_AND_TIME), * toDate("2016-07-16 15:21:01", COMMON_DATE_AND_TIME)); * </pre> * * <b>:</b> * * <pre class="code"> * 1000 * </pre> * * <hr> * * <pre class="code"> * DateExtensionUtil.getIntervalTime( * toDate("2016-07-16 15:21:00", COMMON_DATE_AND_TIME), * toDate("2016-07-16 15:22:00", COMMON_DATE_AND_TIME)); * </pre> * * <b>:</b> * * <pre class="code"> * 60000 * </pre> * * </blockquote> * * @param date1 * date1 * @param date2 * date2 * @return <code>date1</code> null, {@link NullPointerException}<br> * <code>date2</code> null, {@link NullPointerException} * @see DateUtil#getTime(Date) * @see Math#abs(long) * @since 1.6.0 */ public static long getIntervalTime(Date date1, Date date2) { Validate.notNull(date1, "date1 can't be null!"); Validate.notNull(date2, "date2 can't be null!"); return Math.abs(DateUtil.getTime(date2) - DateUtil.getTime(date1)); } // [end] }