com.feilong.core.date.DateExtensionUtilTemp.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.date.DateExtensionUtilTemp.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.feilong.core.date;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.Validate;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.DatePattern;
import com.feilong.tools.jsonlib.JsonUtil;

import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.core.date.DateUtil.toCalendar;
import static com.feilong.core.date.DateUtil.toDate;

import static com.feilong.core.DatePattern.COMMON_DATE;
import static com.feilong.core.DatePattern.COMMON_DATE_AND_TIME;
import static com.feilong.core.DatePattern.COMMON_DATE_AND_TIME_WITHOUT_SECOND;
import static com.feilong.core.DatePattern.COMMON_DATE_AND_TIME_WITHOUT_YEAR_AND_SECOND;
import static com.feilong.core.DatePattern.COMMON_DATE_AND_TIME_WITH_MILLISECOND;
import static com.feilong.core.DatePattern.COMMON_TIME_WITHOUT_SECOND;

/**
 * The Class DateExtensionUtilTest.
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @since 1.0.8
 */
public class DateExtensionUtilTemp extends BaseDateUtilTest {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(DateExtensionUtilTemp.class);

    /** . */
    private static final String WEEK = "";

    /** . */
    private static final String YESTERDAY = "";

    /** ?. */
    private static final String THEDAY_BEFORE_YESTERDAY = "?";

    /** ?. */
    private static final String HOUR = "?";

    /** . */
    private static final String MINUTE = "";

    /** . */
    private static final String SECOND = "";

    /**
     * .<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" };

    /**
     * .
     * 
     * @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];
    }

    /**
     * .
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <pre class="code">
     * DateExtensionUtil.getChineseWeek(0) = 
     * </pre>
     * 
     * </blockquote>
     * 
     * @param week
     *            ,0 ,?1 2 --6
     * @return  
     */
    public static String getChineseWeek(int week) {
        return WEEK + WEEK_CHINESES[week];
    }

    /**
     * Test get chinese week.
     */
    @Test
    public void testGetChineseWeek() {
        LOGGER.debug(getChineseWeek(0));
    }

    /**
     * TestDateUtilTest.
     */
    @Test
    public void testDateUtilTest() {
        List<Date> list = new ArrayList<Date>();
        list.add(new Date());
        list.add(new Date());
        list.add(new Date());
        list.add(new Date());
        LOGGER.debug(JsonUtil.format(toStringList(list, COMMON_DATE_AND_TIME_WITH_MILLISECOND)));
    }

    /**
     * Test to humanization date string.
     */
    @Test
    public void testToPrettyDateString() {
        LOGGER.debug(toPrettyDateString(toDate("2012-10-18 13:55:00", COMMON_DATE_AND_TIME)));
        LOGGER.debug(toPrettyDateString(toDate("2012-10-18 14:14:22", COMMON_DATE_AND_TIME)));
        LOGGER.debug(toPrettyDateString(toDate("2012-10-18 14:15:22", COMMON_DATE_AND_TIME)));
        LOGGER.debug(toPrettyDateString(toDate("2012-10-17 14:15:02", COMMON_DATE_AND_TIME)));
        LOGGER.debug(toPrettyDateString(toDate("2012-10-16 14:15:02", COMMON_DATE_AND_TIME)));
        LOGGER.debug(toPrettyDateString(toDate("2012-10-15 14:15:02", COMMON_DATE_AND_TIME)));
        LOGGER.debug(toPrettyDateString(toDate("2012-09-15 14:15:02", COMMON_DATE_AND_TIME)));
        LOGGER.debug(toPrettyDateString(toDate("2015-08-02 14:15:02", COMMON_DATE_AND_TIME)));
        LOGGER.debug(toPrettyDateString(toDate("2015-7-30 13:00:00", COMMON_DATE_AND_TIME)));
    }

    /**
     * TestCalendarUtilTest.
     */
    @Test
    public void testCalendarUtilTest() {
        List<String> weekDateStringList = getWeekDateStringList(Calendar.THURSDAY,
                COMMON_DATE_AND_TIME_WITH_MILLISECOND);
        LOGGER.debug(JsonUtil.format(weekDateStringList));
    }

    /**
     * ??pattern?.
     * 
     * @param dateList
     *            ?
     * @param datePattern
     *            ? {@link DatePattern}
     * @return  <code>dateList</code> nullempty, {@link Collections#emptyList()}<br>
     *         ?date?string,{@code List<String>}
     */
    public static List<String> toStringList(List<Date> dateList, String datePattern) {
        if (isNullOrEmpty(dateList)) {
            return Collections.emptyList();
        }

        Validate.notBlank(datePattern, "datePattern can't be blank!");

        List<String> stringList = new ArrayList<String>();
        for (Date date : dateList) {
            stringList.add(DateUtil.toString(date, datePattern));
        }
        return stringList;
    }

    /**
     *   <code>week</code> ?.
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <p>
     * ? 2016 ,
     * </p>
     * 
     * <pre class="code">
     * getWeekDateStringList(Calendar.THURSDAY, COMMON_DATE_AND_TIME_WITH_MILLISECOND)
     * </pre>
     * 
     * <b>:</b>
     * 
     * <pre class="code">
     * [
     * "2016-01-07 00:00:00.000",
     * "2016-01-14 00:00:00.000",
     * "2016-01-21 00:00:00.000",
     * "2016-01-28 00:00:00.000",
     * "2016-02-04 00:00:00.000",
     * "2016-02-11 00:00:00.000",
     * "2016-02-18 00:00:00.000",
     * "2016-02-25 00:00:00.000",
     * "2016-03-03 00:00:00.000",
     * "2016-03-10 00:00:00.000",
     * "2016-03-17 00:00:00.000",
     * "2016-03-24 00:00:00.000",
     * "2016-03-31 00:00:00.000",
     * "2016-04-07 00:00:00.000",
     * "2016-04-14 00:00:00.000",
     * "2016-04-21 00:00:00.000",
     * "2016-04-28 00:00:00.000",
     * "2016-05-05 00:00:00.000",
     * "2016-05-12 00:00:00.000",
     * "2016-05-19 00:00:00.000",
     * "2016-05-26 00:00:00.000",
     * "2016-06-02 00:00:00.000",
     * "2016-06-09 00:00:00.000",
     * "2016-06-16 00:00:00.000",
     * "2016-06-23 00:00:00.000",
     * "2016-06-30 00:00:00.000",
     * "2016-07-07 00:00:00.000",
     * "2016-07-14 00:00:00.000",
     * "2016-07-21 00:00:00.000",
     * "2016-07-28 00:00:00.000",
     * "2016-08-04 00:00:00.000",
     * "2016-08-11 00:00:00.000",
     * "2016-08-18 00:00:00.000",
     * "2016-08-25 00:00:00.000",
     * "2016-09-01 00:00:00.000",
     * "2016-09-08 00:00:00.000",
     * "2016-09-15 00:00:00.000",
     * "2016-09-22 00:00:00.000",
     * "2016-09-29 00:00:00.000",
     * "2016-10-06 00:00:00.000",
     * "2016-10-13 00:00:00.000",
     * "2016-10-20 00:00:00.000",
     * "2016-10-27 00:00:00.000",
     * "2016-11-03 00:00:00.000",
     * "2016-11-10 00:00:00.000",
     * "2016-11-17 00:00:00.000",
     * "2016-11-24 00:00:00.000",
     * "2016-12-01 00:00:00.000",
     * "2016-12-08 00:00:00.000",
     * "2016-12-15 00:00:00.000",
     * "2016-12-22 00:00:00.000",
     * "2016-12-29 00:00:00.000"
     * ]
     * </pre>
     * 
     * </blockquote>
     * 
     * @param week
     *            <br>
     *            1 ?2 3 4 5 6 7,<br>
     *             ? {@link Calendar#SUNDAY}, {@link Calendar#MONDAY}, {@link Calendar#TUESDAY},
     *            {@link Calendar#WEDNESDAY}, {@link Calendar#THURSDAY}, {@link Calendar#FRIDAY}, {@link Calendar#SATURDAY}
     * @param datePattern
     *            ??? see {@link DatePattern}
     * @return ?<br>
     *          <code>datePattern</code> null, {@link NullPointerException}<br>
     *          <code>datePattern</code> blank, {@link IllegalArgumentException}
     * @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) {
        Validate.notBlank(datePattern, "datePattern can't be blank!");
        Date now = new Date();
        Date firstWeekOfSpecifyDateYear = DateUtil.getFirstWeekOfSpecifyDateYear(now, week);
        //?
        Calendar calendarEnd = CalendarUtil.resetYearEnd(toCalendar(now));

        List<String> list = new ArrayList<String>();
        Calendar firstWeekOfSpecifyDateYearCalendar = toCalendar(firstWeekOfSpecifyDateYear);
        for (Calendar calendar = firstWeekOfSpecifyDateYearCalendar; calendar.before(calendarEnd); calendar
                .add(Calendar.DAY_OF_YEAR, 7)) {
            list.add(CalendarUtil.toString(calendar, datePattern));
        }
        return list;
    }

    // [start]?? toPrettyDateString

    /**
     * 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?," " + toString(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:inDate{@code <=}? ,???
     * @return date<br>
     *          <code>inDate</code> null, {@link NullPointerException}<br>
     * @see DateUtil#toString(Date, String)
     * @see DateUtil#getYear(Date)
     * @see DateUtil#getDayOfMonth(Date)
     * @see DateUtil#getYear(Date)
     * @see DateExtensionUtil#getIntervalTime(Date, Date)
     * @see DateExtensionUtil#getIntervalDay(long)
     * @see DateExtensionUtil#getIntervalHour(long)
     * @see DateExtensionUtil#getIntervalMinute(long)
     * @see DateExtensionUtil#getIntervalSecond(long)
     * @see org.apache.commons.lang3.time.DurationFormatUtils#formatDurationWords(long, boolean, boolean)
     */
    public static String toPrettyDateString(Date inDate) {
        Validate.notNull(inDate, "inDate can't be null!");

        Date nowDate = new Date();

        // ?
        int inYear = DateUtil.getYear(inDate);
        //**************************************************************************************/
        int currentYear = DateUtil.getYear(nowDate);// ?
        boolean isSameYear = currentYear == inYear;//??
        long spaceTime = DateExtensionUtil.getIntervalTime(inDate, nowDate);// ?
        int spaceDay = DateExtensionUtil.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
            return isSameYear ? DateUtil.toString(inDate, COMMON_DATE_AND_TIME_WITHOUT_YEAR_AND_SECOND)
                    : DateUtil.toString(inDate, COMMON_DATE_AND_TIME_WITHOUT_SECOND);
        }
    }

    // [end]

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

    /**
     * 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) {
        return DateUtil.isEquals(DateUtil.addDay(inDate, 1), nowDate, COMMON_DATE)
                ? YESTERDAY + " " + DateUtil.toString(inDate, COMMON_TIME_WITHOUT_SECOND)
                : THEDAY_BEFORE_YESTERDAY + " " + DateUtil.toString(inDate, 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, COMMON_DATE)) {
            return THEDAY_BEFORE_YESTERDAY + " " + DateUtil.toString(inDate, COMMON_TIME_WITHOUT_SECOND);
        }
        return isSameYear ? DateUtil.toString(inDate, COMMON_DATE_AND_TIME_WITHOUT_YEAR_AND_SECOND)
                : DateUtil.toString(inDate, 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 = DateExtensionUtil.getIntervalHour(spaceTime); // ?
        if (spaceHour == 0) {// ?
            int spaceMinute = DateExtensionUtil.getIntervalMinute(spaceTime);
            return spaceMinute == 0 ? DateExtensionUtil.getIntervalSecond(spaceTime) + SECOND + "?"
                    : spaceMinute + MINUTE + "?";
        }
        // ?
        int inDay = DateUtil.getDayOfMonth(inDate);
        // ?
        int currentDayOfMonth = DateUtil.getDayOfMonth(nowDate);
        return inDay == currentDayOfMonth ? spaceHour + HOUR + "?"
                : YESTERDAY + " " + DateUtil.toString(inDate, COMMON_TIME_WITHOUT_SECOND);
    }
}