com.frey.repo.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.frey.repo.DateUtil.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.frey.repo;

import org.apache.commons.lang.time.DurationFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 */
public class DateUtil implements Serializable {
    public static final String YYYYMMDD_ZH = "yyyyMMdd";
    public static final int FIRST_DAY_OF_WEEK = Calendar.MONDAY; //
    private final static SimpleDateFormat sd1 = new SimpleDateFormat("mm");
    private final static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
    private final static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    //    private final static SimpleDateFormat sdf3T = new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSSZ");
    private final static SimpleDateFormat sdf3T = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    private final static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-M-d");
    private final static SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat sdf6 = new SimpleDateFormat("MM-dd");
    private final static SimpleDateFormat sdf7 = new SimpleDateFormat("HH:mm");
    private final static SimpleDateFormat sdf8 = new SimpleDateFormat("yyyyMMddHH");
    private final static SimpleDateFormat sdf9 = new SimpleDateFormat("yyyyMMddHHmm");
    private final static SimpleDateFormat sdf10 = new SimpleDateFormat("yyyy-MM");
    private final static SimpleDateFormat sdf11 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private static final Log logger = LogFactory.getLog(DateUtil.class);
    private static final String YYYYMMDD = "yyyy-MM-dd";
    private static final SimpleDateFormat mySdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * ??XXXXXX
     */
    public static synchronized String currentDate() {
        return sdf2.format(new Date());
    }

    /**
     * ???2009-08-08
     */
    public static String currentDate2() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    public static Integer currentDateInt() {
        return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
    }

    public static Integer unixTimeToDateInt(long unixTime) {
        return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date(unixTime)));
    }

    public static Integer unixTimeToHourInt(long unixTime) {
        return Integer.parseInt(new SimpleDateFormat("yyyyMMddHH").format(new Date(unixTime))) % 100;
    }

    public static synchronized Integer convertDateStr2Int(String dateString) {
        //    logger.info("DateUtil.convertDateStr2Int...");
        Date date = null;
        try {
            date = sdf3T.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date == null ? null : Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(date));
    }

    /**
     * ???2009-8-8
     */
    public static synchronized String currentDate3() {
        return sdf4.format(new Date());
    }

    /***
     * ??
     */
    public static Date curDate() {
        Calendar cal = Calendar.getInstance();
        return cal.getTime();
    }

    /**
     * ???
     *
     * @return Date
     */
    public static Date curDate5() throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * ??20090807
     */
    public static String yesterdayDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 1);
        return convertDate2String2(cal.getTime());
    }

    /**
     * ??2009-08-07
     */
    public static String yesterdayDate2() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.add(Calendar.DATE, -1);
        return convertDate2String5(cal.getTime());
    }

    /**
     * ??2009-8-7
     */
    public static String yesterdayDate3() {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 1);
        return convertDate2String4(cal.getTime());
    }

    /**
     * ????2011-04-04
     */
    public static String weekAgo() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -8);
        return convertDate2String5(cal.getTime());
    }

    /**
     * ????2011-04-01
     */
    public static String monthAgo() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -30);
        return convertDate2String5(cal.getTime());
    }

    /**
     * ?2012-08-08?2012-08-09 00:00:00,0
     */
    public static Date convertDateForDawn(Date date) {
        if (date != null) {
            date = DateUtil.addDay(date, 1);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR_OF_DAY, 0);
            calendar.add(Calendar.MINUTE, 0);
            calendar.add(Calendar.SECOND, 0);
            calendar.add(Calendar.MILLISECOND, 0);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    /**
     * ???2009-08-08 10:10:01
     */
    public static String currentDateTime() {

        return convertDate2String(new Date());
    }

    public static String currentTDateTime() {

        return convertDate2TString(new Date());
    }

    /**
     * ?? 2009-08-08 10:10:01?
     */
    public static String convertDate2String(Date date) {
        if (date == null) {
            return "";
        }
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * ?? 2009-08-08 10:10:01?
     */
    public static String convertDate2TString(Date date) {
        if (date == null) {
            return "";
        }
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
    }

    /**
     * ?? 20090808 ?
     */
    public static synchronized String convertDate2String2(Date date) {
        if (date == null) {
            return "";
        }

        return sdf2.format(date);
    }

    /**
     * ?? 2009-08-08 10:10:01.988?
     */
    public static synchronized String convertDate2String3(Date date) {
        if (date == null) {
            return "";
        }

        return sdf3.format(date);
    }

    /**
     * ?? 2009-8-8 ?
     */
    public static synchronized String convertDate2String4(Date date) {
        if (date == null) {
            return "";
        }

        return sdf4.format(date);
    }

    /**
     * ?? 2009-08-08 ?
     */
    public static synchronized String convertDate2String5(Date date) {
        if (date == null) {
            return "";
        }
        //        SimpleDateFormat mySdf = new SimpleDateFormat("yyyy-MM-dd");
        return mySdf.format(date);
    }

    /**
     * ?? 08-08 ?
     */
    public static synchronized String convertDate2String6(Date date) {
        if (date == null) {
            return "";
        }

        return sdf6.format(date);
    }

    /**
     * ??2012- 08 ?
     */
    public static synchronized String convertDate2String10(Date date) {
        if (date == null) {
            return "";
        }

        return sdf10.format(date);
    }

    /**
     * ??2012- 08 ?
     */
    public static synchronized String convertDate2String11(Date date) {
        if (date == null) {
            return "";
        }

        return sdf11.format(date);
    }

    /**
     * ?? HH:mm ?
     */
    public static synchronized String convertDate2String7(Date date) {
        if (date == null) {
            return "";
        }

        return sdf7.format(date);
    }

    /**
     * yyyyMMddHH
     */
    public static synchronized String convertDate2String8(Date time) {
        return sdf8.format(time);
    }

    /**
     * yyyyMMddHHmm
     */
    public static synchronized String convertDate2String9(Date time) {
        return sdf9.format(time);
    }

    /**
     * mm
     */
    public static synchronized String getMininte(Date time) {
        return sd1.format(time);
    }

    /**
     * 2009-8-8 23:59:50???
     */
    public static Date stringToDate(String date) throws Exception {
        if (date == null || "".equals(date))
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
        return sdf.parse(date);
    }

    /**
     * 2009-8-8???
     */
    public static Date stringToDate2(String date) {
        if (date == null || "".equals(date))
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * @param date 2011-04-01?
     * @return 
     */
    public static Date stringToDate3(String date) {
        if (date == null || "".equals(date))
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * @param date 201104012300?
     * @return 
     */
    public static Date stringToDate4(String date) {
        if (date == null || "".equals(date))
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 2009-08-08 23:59:50???
     */
    public static Date stringToDate5(String date) throws Exception {
        if (date == null || "".equals(date))
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(date);

    }

    /**
     * 2009-08???
     */
    public static Date stringToDate10(String date) throws Exception {
        if (date == null || "".equals(date))
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.parse(date);

    }

    /**
     * @param currentTime ??
     * @param days        ??
     * @return ??,?2011-05-11
     */
    public static String stringSomeDaysAgo(Date currentTime, int days) {
        if (currentTime == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentTime);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - days);
        return convertDate2String5(cal.getTime());

    }

    /**
     * @param currentTime ??
     * @param days        ??
     * @return ??,Date
     */
    public static Date dateSomeDaysAgo(Date currentTime, int days) {
        if (currentTime == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentTime);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - days);
        return cal.getTime();
    }

    /**
     * ??
     */
    public static String dateSomeYearLaterStr(Date currentTime, int years) {
        if (currentTime == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentTime);
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + years);
        return convertDate2String5(cal.getTime());
    }

    public static Date dateSomeYearLaterDate(Date currentTime, int years) {
        if (currentTime == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentTime);
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + years);
        return cal.getTime();
    }

    /**
     * @param currentTime ??
     * @param min         ??
     * @return ??,Date
     */
    public static Date dateSomeMinLater(Date currentTime, int min) {
        if (currentTime == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentTime);
        cal.add(Calendar.MINUTE, min);
        return cal.getTime();
    }

    public static String setDateFormat(String myDate, String strFormat) {

        SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
        String sDate = "";
        try {
            sDate = sdf.format(sdf.parse(myDate));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sDate;
    }

    /*****************************************
     * @return interger
     * @ ?
     ****************************************/
    public static int getWeekNumOfYearDay(String strDate) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = format.parse(strDate);
        int iWeekNum;
        calendar.setTime(curDate);
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR) - 1;
        } else {
            iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
        }
        return iWeekNum;
    }

    /*****************************************
     * @return interger
     * @ ??
     ****************************************/
    public static String getYearWeekFirstDay(int yearNum, int weekNum) throws ParseException {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, yearNum);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        // ????
        String tempYear = Integer.toString(yearNum);
        String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
        String tempDay = Integer.toString(cal.get(Calendar.DATE));
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return setDateFormat(tempDate, "yyyy-MM-dd");

    }

    /*****************************************
     * @return interger
     * @ ???
     ****************************************/
    public static String getYearWeekEndDay(int yearNum, int weekNum) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, yearNum);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum + 1);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        // ????
        String tempYear = Integer.toString(yearNum);
        String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
        String tempDay = Integer.toString(cal.get(Calendar.DATE));
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return setDateFormat(tempDate, "yyyy-MM-dd");
    }

    /*****************************************
     * @return interger
     * @ ??
     ****************************************/
    public static String getYearMonthFirstDay(int yearNum, int monthNum) {

        // ????
        String tempYear = Integer.toString(yearNum);
        String tempMonth = Integer.toString(monthNum);
        String tempDay = "1";
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return setDateFormat(tempDate, "yyyy-MM-dd");

    }

    /*****************************************
     * @return interger
     * @ ???
     ****************************************/
    public static String getYearMonthEndDay(int yearNum, int monthNum) {

        // ????
        String tempYear = Integer.toString(yearNum);
        String tempMonth = Integer.toString(monthNum);
        String tempDay = "31";
        if (tempMonth.equals("1") || tempMonth.equals("3") || tempMonth.equals("5") || tempMonth.equals("7")
                || tempMonth.equals("8") || tempMonth.equals("10") || tempMonth.equals("12")) {
            tempDay = "31";
        }
        if (tempMonth.equals("4") || tempMonth.equals("6") || tempMonth.equals("9") || tempMonth.equals("11")) {
            tempDay = "30";
        }
        if (tempMonth.equals("2")) {
            if (isLeapYear(yearNum)) {
                tempDay = "29";
            } else {
                tempDay = "28";
            }
        }
        // System.out.println("tempDay:" + tempDay);
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return setDateFormat(tempDate, "yyyy-MM-dd");
    }

    /*****************************************
     * @return boolean
     * @ ??
     ****************************************/
    public static boolean isLeapYear(int yearNum) {
        boolean isLeep = false;
        /** ?flag */
        if ((yearNum % 4 == 0) && (yearNum % 100 != 0)) {
            isLeep = true;
        } else if (yearNum % 400 == 0) {
            isLeep = true;
        } else {
            isLeep = false;
        }
        return isLeep;
    }

    /*****************************************
     * @ ??
     ****************************************/
    public static String getYearWeekFirstDay(Date curDate) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(curDate);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            cal.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR) - 1);
        } else {
            cal.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR));
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String tempYear = Integer.toString(cal.get(Calendar.YEAR));
        String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
        String tempDay = Integer.toString(cal.get(Calendar.DATE));
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return setDateFormat(tempDate, "yyyy-MM-dd");

    }

    public static Date getYearWeekFirstDayDate(Date curDate) {
        return stringToDate3(getYearWeekFirstDay(curDate));
    }

    /*****************************************
     * @ ???
     ****************************************/
    public static String getYearWeekEndDay(Date curDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(curDate);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            cal.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR));
        } else {
            cal.set(Calendar.WEEK_OF_YEAR, cal.get(Calendar.WEEK_OF_YEAR) + 1);
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        // cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY+1);
        // ????
        String tempYear = Integer.toString(cal.get(Calendar.YEAR));
        String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
        String tempDay = Integer.toString(cal.get(Calendar.DATE));
        String tempDate = tempYear + "-" + tempMonth + "-" + tempDay;
        return setDateFormat(tempDate, "yyyy-MM-dd");
    }

    public static Date getYearWeekEndDayDate(Date curDate) {
        return stringToDate3(getYearWeekEndDay(curDate));
    }

    /*****************************************
     * @return interger
     * @ ??
     ****************************************/
    public static String getYearMonthFirstDay(Date curDate) {
        if (curDate == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(curDate);
        int yearNum = cal.get(Calendar.YEAR);
        int monthNum = cal.get(Calendar.MONTH) + 1;
        return getYearMonthFirstDay(yearNum, monthNum);
    }

    public static Date getYearMonthFirstDayDate(Date curDate) {
        return stringToDate3(getYearMonthFirstDay(curDate));
    }

    /*****************************************
     * @return interger
     * @ ???
     ****************************************/
    public static String getYearMonthEndDay(Date curDate) {
        if (curDate == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(curDate);
        int yearNum = cal.get(Calendar.YEAR);
        int monthNum = cal.get(Calendar.MONTH) + 1;
        return getYearMonthEndDay(yearNum, monthNum);
    }

    public static Date getYearMonthEndDayDate(Date curDate) {
        return stringToDate3(getYearMonthEndDay(curDate));
    }

    public static String getYearByDate(Date curDate) {
        if (curDate == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(curDate);
        int yearNum = cal.get(Calendar.YEAR);
        return Integer.toString(yearNum);
    }

    public static String getMontByDate(Date curDate) {
        if (curDate == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(curDate);
        int monthNum = cal.get(Calendar.MONTH) + 1;
        return Integer.toString(monthNum);
    }

    public static int getWeekNumOfYearDay(Date curDate) {
        if (curDate == null)
            return 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(curDate);
        int iWeekNum;
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR) - 1;
        } else {
            iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
        }
        // int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
        return iWeekNum;
    }

    /**
     * 
     */
    public static Date addDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 
     */
    public static Date addMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    /**
     * 
     */
    public static String addDayString5(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return convertDate2String5(calendar.getTime());
    }

    /**
     * 
     */
    public static String addMonthString5(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return convertDate2String5(calendar.getTime());
    }

    /**
     * 
     */
    public static int compTowData(Date date1, Date date2) {
        String date_str1 = convertDate2String5(date1);
        String date_str2 = convertDate2String5(date2);
        Date d1 = stringToDate3(date_str1);
        Date d2 = stringToDate3(date_str2);
        Long l;
        if (d1.getTime() > d2.getTime()) {
            l = d1.getTime() - d2.getTime();
        } else {
            l = d2.getTime() - d1.getTime();
        }
        String daystr = DurationFormatUtils.formatDuration(l, "d");
        return Integer.parseInt(daystr);
    }

    public static Long compTwoData(String date_str1, String date_str2) {
        //String date_str1 = convertDate2String5(date1);
        //String date_str2 = convertDate2String5(date2);
        Date d1 = stringToDate3(date_str1);
        Date d2 = stringToDate3(date_str2);
        Long l;
        if (d1.getTime() > d2.getTime()) {
            l = d1.getTime() - d2.getTime();
        } else {
            l = d2.getTime() - d1.getTime();
        }
        //      String day = DurationFormatUtils.formatDuration(l, "d");

        return l;
    }

    /**
     * @param strDate
     * @return
     */
    public static Date parseDate(String strDate) {
        return parseDate(strDate, null);
    }

    /**
     * parseDate
     */
    public static Date parseDate(String strDate, String pattern) {
        Date date = null;
        try {
            if (pattern == null) {
                pattern = YYYYMMDD;
            }
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            date = format.parse(strDate);
        } catch (Exception e) {
            logger.error("parseDate error:" + e);
        }
        return date;
    }

    /**
     * format date
     */
    public static String formatDate(Date date) {
        return formatDate(date, null);
    }

    /**
     * format date
     *
     * @param pattern ?yyyy-MM-dd
     */
    public static String formatDate(Date date, String pattern) {
        String strDate = null;
        try {
            if (pattern == null) {
                pattern = YYYYMMDD;
            }
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            strDate = format.format(date);
        } catch (Exception e) {
            logger.error("formatDate error:", e);
        }
        return strDate;
    }

    /**
     * ?
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week_of_year = c.get(Calendar.WEEK_OF_YEAR);
        return week_of_year - 1;
    }

    /**
     * ?15?15? <p/>
     * <pre>
     * 2012-01-01-2012-02-14 1
     * 2012-01-01-2012-02-15 1
     * 2012-01-01-2012-02-16 2
     * </pre>
     */
    public static Integer getMonthBetweenBeginDateAndEndDate(Date beginDate, Date endDate) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(beginDate);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endDate);
        int year = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
        int month = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
        int day = cal2.get(Calendar.DAY_OF_MONTH) - cal1.get(Calendar.DAY_OF_MONTH);
        month = year * 12 + month;
        if (day > 15) {// 15
            month++;
        } else if (day <= -15) {// ?15
            month--;
        }
        return month;
    }

    /**
     * @return ??
     * @desc ??
     * @author:xiaodi@qiyi.com
     * @date: 2013-3-14
     */
    public static Integer getMonthsBetweenBeginDateAndEndDate(Date beginDate, Date endDate) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(beginDate);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endDate);
        int year = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
        int month = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
        month = year * 12 + month;
        month += 1;
        return month;
    }

    /**
     * getWeekBeginAndEndDate
     */
    public static String getWeekBeginAndEndDate(Date date, String pattern) {
        Date monday = getMondayOfWeek(date);
        Date sunday = getSundayOfWeek(date);
        return formatDate(monday, pattern) + " - " + formatDate(sunday, pattern);
    }

    /**
     * ??
     */
    public static Date getMondayOfWeek(Date date) {
        Calendar monday = Calendar.getInstance();
        monday.setTime(date);
        monday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
        monday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return monday.getTime();
    }

    /**
     * ??
     */
    public static Date getSundayOfWeek(Date date) {
        Calendar sunday = Calendar.getInstance();
        sunday.setTime(date);
        sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
        sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sunday.getTime();
    }

    /**
     * ?
     */
    public static int getRemainDayOfMonth(Date date) {
        int dayOfMonth = getDayOfMonth(date);
        int day = getPassDayOfMonth(date);
        return dayOfMonth - day;
    }

    /**
     * ??
     */
    public static int getPassDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * ?
     */
    public static int getDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * ?
     */
    public static Date getFirstDateOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * ??
     */
    public static Date getLastDateOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * ?
     */
    public static Date getFirstDateOfSeason(Date date) {
        return getFirstDateOfMonth(getSeasonDate(date)[0]);
    }

    /**
     * ?
     */
    public static String getFirstDateOfSeasonStr(Date date) {
        return convertDate2String5(getFirstDateOfMonth(getSeasonDate(date)[0]));
    }

    /**
     * ??
     */
    public static Date getLastDateOfSeason(Date date) {
        return getLastDateOfMonth(getSeasonDate(date)[2]);
    }

    /**
     * ??
     */
    public static String getLastDateOfSeasonStr(Date date) {
        return convertDate2String5(getLastDateOfSeason(getSeasonDate(date)[0]));
    }

    /**
     * ?
     */
    public static int getDayOfSeason(Date date) {
        int day = 0;
        Date[] seasonDates = getSeasonDate(date);
        for (Date date2 : seasonDates) {
            day += getDayOfMonth(date2);
        }
        return day;
    }

    /**
     * ?
     */
    public static int getRemainDayOfSeason(Date date) {
        return getDayOfSeason(date) - getPassDayOfSeason(date);
    }

    /**
     * ?
     */
    public static int getPassDayOfSeason(Date date) {
        int day = 0;

        Date[] seasonDates = getSeasonDate(date);

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);

        if (month == Calendar.JANUARY || month == Calendar.APRIL || month == Calendar.JULY
                || month == Calendar.OCTOBER) {//
            day = getPassDayOfMonth(seasonDates[0]);
        } else if (month == Calendar.FEBRUARY || month == Calendar.MAY || month == Calendar.AUGUST
                || month == Calendar.NOVEMBER) {//
            day = getDayOfMonth(seasonDates[0]) + getPassDayOfMonth(seasonDates[1]);
        } else if (month == Calendar.MARCH || month == Calendar.JUNE || month == Calendar.SEPTEMBER
                || month == Calendar.DECEMBER) {//
            day = getDayOfMonth(seasonDates[0]) + getDayOfMonth(seasonDates[1]) + getPassDayOfMonth(seasonDates[2]);
        }
        return day;
    }

    /**
     * ?
     */
    public static Date[] getSeasonDate(Date date) {
        Date[] season = new Date[3];

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int nSeason = getSeason(date);
        if (nSeason == 1) {//
            c.set(Calendar.MONTH, Calendar.JANUARY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.FEBRUARY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MARCH);
            season[2] = c.getTime();
        } else if (nSeason == 2) {//
            c.set(Calendar.MONTH, Calendar.APRIL);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MAY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.JUNE);
            season[2] = c.getTime();
        } else if (nSeason == 3) {//
            c.set(Calendar.MONTH, Calendar.JULY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.AUGUST);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.SEPTEMBER);
            season[2] = c.getTime();
        } else if (nSeason == 4) {//
            c.set(Calendar.MONTH, Calendar.OCTOBER);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.NOVEMBER);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.DECEMBER);
            season[2] = c.getTime();
        }
        return season;
    }

    /**
     * 1  2  3  4 
     */
    public static int getSeason(Date date) {

        int season = 0;

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        switch (month) {
        case Calendar.JANUARY:
        case Calendar.FEBRUARY:
        case Calendar.MARCH:
            season = 1;
            break;
        case Calendar.APRIL:
        case Calendar.MAY:
        case Calendar.JUNE:
            season = 2;
            break;
        case Calendar.JULY:
        case Calendar.AUGUST:
        case Calendar.SEPTEMBER:
            season = 3;
            break;
        case Calendar.OCTOBER:
        case Calendar.NOVEMBER:
        case Calendar.DECEMBER:
            season = 4;
            break;
        default:
            break;
        }
        return season;
    }

    public static int compareDateDay(String DATE1, String DATE2) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 1?0
     *
     * @return ??
     * @desc ??
     * @author:xiaodi@qiyi.com
     * @date: 2013-3-15
     */
    public static int compareDateDay(Date DATE1, Date DATE2) {

        try {
            if (DATE1.getTime() > DATE2.getTime()) {
                return 1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * ?
     */
    public static int getDateBeginAndEndDate(Date DATE1, Date DATE2) {
        try {
            long time = DATE2.getTime() - DATE1.getTime();
            return (int) (time / 86400000 + 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * ?
     */
    public static int getDateBeginAndEndDate(String DATE1, String DATE2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            int time = getDateBeginAndEndDate(dt1, dt2);
            return time;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static synchronized String unixTimeToDateString(long unixTime) {
        if ((unixTime + "").length() < 13) {
            unixTime *= 1000;
        }
        return sdf1.format(new Date(unixTime));
    }

    public static synchronized String unixTimeToDateStringAndT(long unixTime) {
        if ((unixTime + "").length() < 13) {
            unixTime *= 1000;
        }
        return sdf3T.format(new Date(unixTime));
    }

    private static long getRandomUnixTime() {
        return (long) (Math.random() * 1555200) + 1434729600;
    }

    public static void main(String args[]) {
        //      System.out.println("getFirstDateOfSeason=" + getFirstDateOfSeason(new Date()));
        //      System.out.println("getFirstDateOfMonth=" + getFirstDateOfMonth(new Date()));
        //      System.out.println("getFirstDateOfMonth=" + getFirstDateOfMonth(new Date()));
        //      System.out.println("getFirstDateOfSeasonStr=" + getFirstDateOfSeasonStr(new Date()));
        //      System.out.println("getLastDateOfSeasonStr=" + getLastDateOfSeasonStr(new Date()));
        //      System.out.println("getYearMonthFirstDay=" + getYearMonthFirstDay(new Date()));
        //      System.out.println("getYearWeekEndDay=" + getYearWeekEndDay(new Date()));
        //      System.out.println("getYearWeekFirstDay=" + getYearWeekFirstDay(new Date()));
        System.out.println(getDateBeginAndEndDate(new Date(), getLastDateOfMonth(new Date())));
        for (int i = 0; i < 100; i++) {
            System.out.println(unixTimeToDateString(getRandomUnixTime()));
        }

    }

}