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.discovery.darchrow.date; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.List; import com.discovery.darchrow.util.Validator; /** * . * <p> * DateUtil ,DateUtil ?Date API,DateExtensionUtil ,. * </p> * * <h3></h3> * * <blockquote> * <ul> * <li>{@link #getIntervalDayList(String, String, String)}</li> * <li>{@link #getIntervalForView(long)}</li> * <li>{@link #getIntervalForView(Date, Date)}</li> * </ul> * </blockquote> * * @author feilong * @version 1.0.8 2014731 ?2:34:33 * @since 1.0.8 */ public final class DateExtensionUtil { /** . */ public static final String YESTERDAY = ""; /** ?. */ public static final String THEDAY_BEFORE_YESTERDAY = "?"; /** . */ public static final String WEEK = ""; /** . */ public static final String DAY = ""; /** ?. */ public static final String HOUR = "?"; /** . */ public static final String MINUTE = ""; /** . */ public static final String SECOND = ""; /** . */ public static final String MILLISECOND = ""; /** * .<br> * { "", "", "", "", "", "", "" } */ private static final String[] WEEK_CHINESES = { "", "", "", "", "", "", "" }; /** * .<br> * { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" } */ private static final String[] WEEK_ENGLISHS = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; /** Don't let anyone instantiate this class. */ private DateExtensionUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } /** * . * * @param week * 0 1 2 --6 * @return Sunday { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" } */ public static String getEnglishWeek(int week) { return WEEK_ENGLISHS[week]; } /** * . * * @param week * 0 1 2 --6 * @return */ public static String getChineseWeek(int week) { return WEEK + WEEK_CHINESES[week]; } // [start]extent /,?sql /** * 0:00:00?0:00:00,?,between ... and ... * * <pre> * 2012-10-16 22:18:34 * * return {2012-10-16 00:00:00.000,2012-10-17 00:00:00.000} * </pre> * * @return Date today tomorrow * @since 1.0 * @deprecated ???? */ @Deprecated public static Date[] getExtentToday() { Calendar calendar = CalendarUtil.resetCalendarByDay(new Date()); Date today = calendar.getTime(); // *************************** calendar.add(Calendar.DATE, 1); Date tomorrow = calendar.getTime(); return new Date[] { today, tomorrow }; } /** * [yestoday,today]<br> * 00:00 <br> * 00:00 <br> * sql/hql?,between ... and ... * * <pre> * :2012-10-16 22:46:42 * * return {2012-10-15 00:00:00.000,2012-10-16 00:00:00.000} * </pre> * * @return Date <br> * 00:00 <br> * 00:00 * @since 1.0 * @deprecated ?? */ @Deprecated public static Date[] getExtentYesterday() { Calendar calendar = CalendarUtil.resetCalendarByDay(new Date()); Date today = calendar.getTime(); calendar.add(Calendar.DATE, -1); Date yesterday = calendar.getTime(); return new Date[] { yesterday, today }; } // [end] /** * ?(??),<br> * ?? <code>00:00:00.000</code> * * <pre> * getIntervalDayList("2011-03-5 23:31:25.456","2011-03-10 01:30:24.895", DatePattern.commonWithTime) * * return * 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> * * @param fromDateString * * @param toDateString * ? * @param datePattern * ? {@link DatePattern} * @return the interval day list * @see DateUtil#getIntervalDay(Date, Date) */ public static List<Date> getIntervalDayList(String fromDateString, String toDateString, String datePattern) { List<Date> dateList = new ArrayList<Date>(); //***************************************************************/ Date beginDate = DateUtil.string2Date(fromDateString, datePattern); Date endDate = DateUtil.string2Date(toDateString, datePattern); // ******?******** Date beginDateReset = CalendarUtil.resetDateByDay(beginDate); Date endDateReset = CalendarUtil.resetDateByDay(endDate); //***************************************************************/ // int intervalDay = DateUtil.getIntervalDay(beginDateReset, endDateReset); //***************************************************************/ Date minDate = beginDateReset; if (beginDateReset.equals(endDateReset)) { minDate = beginDateReset; } else if (beginDateReset.before(endDateReset)) { minDate = beginDateReset; } else { minDate = endDateReset; } //***************************************************************/ dateList.add(minDate); //***************************************************************/ if (intervalDay > 0) { for (int i = 0; i < intervalDay; ++i) { dateList.add(DateUtil.addDay(minDate, i + 1)); } } return dateList; } /** * ? :getWeekDateStringList(6, "yyyy-MM-dd");. * * @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} * @param datePattern * ??? * @return ? * @see org.apache.commons.lang3.time.DateUtils#iterator(Date, int) * @see Calendar#SUNDAY * @see Calendar#MONDAY * @see Calendar#TUESDAY * @see Calendar#WEDNESDAY * @see Calendar#THURSDAY * @see Calendar#FRIDAY * @see Calendar#SATURDAY */ public static List<String> getWeekDateStringList(int week, String datePattern) { Date now = new Date(); Date firstWeekOfSpecifyDateYear = DateUtil.getFirstWeekOfSpecifyDateYear(now, week); //? Calendar calendarEnd = CalendarUtil.resetYearEnd(DateUtil.toCalendar(now)); List<String> list = new ArrayList<String>(); for (Calendar calendar = DateUtil.toCalendar(firstWeekOfSpecifyDateYear); calendar .before(calendarEnd); calendar.add(Calendar.DAY_OF_YEAR, 7)) { list.add(CalendarUtil.toString(calendar, datePattern)); } return list; } // [start]?? toHumanizationDateString /** * date,??. * * <p> * ?,inDate new Date()?;,(inDate?? ,???) * </p> * * <ul> * <li>0,<br> * ?0,0, + "?"<br> * ?0,?0, + "?"<br> * </li> * <li>0,<br> * ??0,inDateday currentday ,space_hour + "??"<br> * ??0,inDateday currentday?," " + date2String(inDate, "HH:mm")<br> * </li> * <li>1,inDateday+1currentDateday ," HH:mm"</li> * <li>1,inDateday+1currentDateday ?,"? HH:mm"</li> * <li>2,inDateday+2currentDateday ,"? HH:mm"</li> * <li>2,inDateday+2currentDateday ?,<br> * 1).inDateyearcurrentDateyear,"MM-dd HH:mm"<br> * 2).inDateyearcurrentDateyear?,"yyyy-MM-dd HH:mm"</li> * <li>2<br> * 1).inDateyearcurrentDateyear,"MM-dd HH:mm"<br> * 2).inDateyearcurrentDateyear?,"yyyy-MM-dd HH:mm"</li> * </ul> * * @param inDate * ?<br> * warn:{@code inDate<=?} ,??? * @return date * @see DateUtil#date2String(Date, String) * @see DateUtil#getYear(Date) * @see DateUtil#getDayOfMonth(Date) * @see DateUtil#getYear(Date) * @see DateUtil#getIntervalTime(Date, Date) * @see DateUtil#getIntervalDay(long) * @see DateUtil#getIntervalHour(long) * @see DateUtil#getIntervalMinute(long) * @see DateUtil#getIntervalSecond(long) */ public static String toPrettyDateString(Date inDate) { Date nowDate = new Date(); // ? int inYear = DateUtil.getYear(inDate); //**************************************************************************************/ int currentYear = DateUtil.getYear(nowDate);// ? boolean isSameYear = currentYear == inYear;//?? long spaceTime = DateUtil.getIntervalTime(inDate, nowDate);// ? int spaceDay = DateUtil.getIntervalDay(spaceTime);// //**************************************************************************************/ switch (spaceDay) { case 0: // 0 return doWithZeroDayInterval(inDate, nowDate, spaceTime); case 1: // return doWithOneDayInterval(inDate, nowDate); case 2: // 2 return doWithTwoDaysInterval(inDate, nowDate, isSameYear); default://spaceDay > 2 // 2 if (isSameYear) { return DateUtil.date2String(inDate, DatePattern.COMMON_DATE_AND_TIME_WITHOUT_YEAR_AND_SECOND); } return DateUtil.date2String(inDate, DatePattern.COMMON_DATE_AND_TIME_WITHOUT_SECOND); } } /** * Do with one day interval. * * @param inDate * the in date * @param nowDate * the now date * @return the string * @since 1.4.0 */ private static String doWithOneDayInterval(Date inDate, Date nowDate) { if (DateUtil.isEquals(DateUtil.addDay(inDate, 1), nowDate, DatePattern.COMMON_DATE)) { return YESTERDAY + " " + DateUtil.date2String(inDate, DatePattern.COMMON_TIME_WITHOUT_SECOND); } return THEDAY_BEFORE_YESTERDAY + " " + DateUtil.date2String(inDate, DatePattern.COMMON_TIME_WITHOUT_SECOND); } /** * Do with two days interval. * * @param inDate * the in date * @param nowDate * the now date * @param isSameYear * the is same year * @return the string * @since 1.4.0 */ private static String doWithTwoDaysInterval(Date inDate, Date nowDate, boolean isSameYear) { if (DateUtil.isEquals(DateUtil.addDay(inDate, 2), nowDate, DatePattern.COMMON_DATE)) { return THEDAY_BEFORE_YESTERDAY + " " + DateUtil.date2String(inDate, DatePattern.COMMON_TIME_WITHOUT_SECOND); } if (isSameYear) { return DateUtil.date2String(inDate, DatePattern.COMMON_DATE_AND_TIME_WITHOUT_YEAR_AND_SECOND); } return DateUtil.date2String(inDate, DatePattern.COMMON_DATE_AND_TIME_WITHOUT_SECOND); } /** * Do with zero day interval. * * @param inDate * the in date * @param nowDate * the now date * @param spaceTime * the space time * @return the string * @since 1.4.0 */ private static String doWithZeroDayInterval(Date inDate, Date nowDate, long spaceTime) { int spaceHour = DateUtil.getIntervalHour(spaceTime); // ? if (spaceHour == 0) {// ? int spaceMinute = DateUtil.getIntervalMinute(spaceTime); if (spaceMinute == 0) { int spaceSecond = DateUtil.getIntervalSecond(spaceTime); return spaceSecond + SECOND + "?"; } return spaceMinute + MINUTE + "?"; } // ? int inDay = DateUtil.getDayOfMonth(inDate); // ? int currentDayOfMonth = DateUtil.getDayOfMonth(nowDate); if (inDay == currentDayOfMonth) { return spaceHour + HOUR + "?"; } return YESTERDAY + " " + DateUtil.date2String(inDate, DatePattern.COMMON_TIME_WITHOUT_SECOND); } // [end] /** * ??pattern?. * * @param dateList * ? * @param datePattern * ? {@link DatePattern} * * @return Validator.isNotNullOrEmpty(dateList) return null;<br> * ?date?string,{@code List<String>} */ public static List<String> toStringList(List<Date> dateList, String datePattern) { if (Validator.isNullOrEmpty(dateList)) { return Collections.emptyList(); } List<String> stringList = new ArrayList<String>(); for (Date date : dateList) { stringList.add(DateUtil.date2String(date, datePattern)); } return stringList; } /** * (??),???. * * <pre> * getIntervalForView(13516) * return 13516 * * getIntervalForView(0) * return 0 * * ,?,,, * </pre> * * @param spaceTime * ?? * @return (??),???<br> * space_time 0 0 * @see DateUtil#getIntervalDay(long) * @see DateUtil#getIntervalHour(long) * @see DateUtil#getIntervalMinute(long) * @see DateUtil#getIntervalSecond(long) */ public static String getIntervalForView(long spaceTime) { if (0 == spaceTime) { return "0"; } // ************************************************************************************** // long spaceDay = DateUtil.getIntervalDay(spaceTime); // ? ??, long spaceHour = DateUtil.getIntervalHour(spaceTime) - spaceDay * 24; // ????, long spaceMinute = DateUtil.getIntervalMinute(spaceTime) - (spaceDay * 24 + spaceHour) * 60; // ???,?, long spaceSecond = DateUtil.getIntervalSecond(spaceTime) - ((spaceDay * 24 + spaceHour) * 60 + spaceMinute) * 60; // ???,?, long spaceMillisecond = spaceTime - (((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(); } /** * ,???. * * <pre> * getIntervalForView(2011-05-19 8:30:40,2011-05-19 11:30:24) * return ??2?5944 * * getIntervalForView(2011-05-19 11:31:25.456,2011-05-19 11:30:24.895) * return 11 * * ,?,,, * </pre> * * @param date1 * 1 * @param date2 * 2 * @return ,??? * @see #getIntervalForView(long) * @see DateUtil#getIntervalTime(Date, Date) */ public static String getIntervalForView(Date date1, Date date2) { long spaceTime = DateUtil.getIntervalTime(date1, date2); return getIntervalForView(spaceTime); } }