日期工具类


一、日期加减

public class DateFormatUtil {

    /**
     * 日期时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN_FORMAT = "yyyy-MM-dd";

    /**
     * 将Date类型日期转为字符串类型
     * @param date 日期类型
     * @return
     */
    public static String format(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_FORMAT);
            return sdf.format(date);
        }catch (Exception e1){
            e1.printStackTrace();
            return null;
        }
    }

    /**
     * 对日期的【月】进行加/减
     *
     * @param date 日期
     * @param months 月数,负数为减
     * @return 加/减几月后的日期
     */
    public static Date addDateMonths(Date date, int months) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();
    }
    /**
     * 对日期的【小时】进行加/减
     *
     * @param date 日期
     * @param hours 小时数,负数为减
     * @return 加/减几小时后的日期
     */
    public static Date addDateHours(Date date, int hours) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }
    /**
     * 对日期的【天】进行加/减
     *
     * @param date 日期
     * @param days 天数,负数为减
     * @return 加/减几天后的日期
     */
    public static Date addDateDays(Date date, int days) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }
    /**
     * 获取两个日期之间的所有日期
     * @param start_date
     * @param end_date
     * @return
     */
    public static List<String> getBetweenDates(Date start_date, Date end_date) {
        List<String> result = new ArrayList<String>();
      /*  try {*/
             //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
           /* Date start_date = sdf.parse(start);
            Date end_date = sdf.parse(end);*/
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start_date);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end_date);
            while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
                if(tempStart.get(Calendar.DATE)<10){
                    result.add("0"+tempStart.get(Calendar.DATE));
                }else{
                    result.add(""+tempStart.get(Calendar.DATE));
                }
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
       /* } catch (ParseException e) {
            e.printStackTrace();
        }*/
        //Collections.reverse(result);
        return result;
    }

    /**
     * 获取两个日期之间的所有日期(m-d)
     * @param start_date
     * @param end_date
     * @return
     */
    public static List<String> getBetweenDatesMD(Date start_date, Date end_date) {
        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start_date);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end_date);
        String month = "";
        while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
            if((tempStart.get(Calendar.MONTH)+1)<10){
                month = "0"+(tempStart.get(Calendar.MONTH)+1);
            }else {
                month = ""+(tempStart.get(Calendar.MONTH)+1);
            }
            if(tempStart.get(Calendar.DATE)<10){
                result.add(month + "-" + "0" + tempStart.get(Calendar.DATE));
            }else{
                result.add(month + "-" + tempStart.get(Calendar.DATE));
            }
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    /**
     * 获取两个日期之间的所有日期
     * @param start_date
     * @param end_date
     * @return
     */
    public static List<String> getBetweenYearMonthDates(Date start_date, Date end_date) {
        List<String> result = new ArrayList<String>();
        /*  try {*/
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
           /* Date start_date = sdf.parse(start);
            Date end_date = sdf.parse(end);*/
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start_date);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end_date);
        String month = "";
        while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
            if((tempStart.get(Calendar.MONTH)+1)<10){
                month = "0"+(tempStart.get(Calendar.MONTH)+1);
            }else {
                month = ""+(tempStart.get(Calendar.MONTH)+1);
            }
            if(tempStart.get(Calendar.DATE)<10){
                result.add(month+"-"+"0"+tempStart.get(Calendar.DATE));
            }else{
                result.add(month+"-"+tempStart.get(Calendar.DATE));
            }
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
       /* } catch (ParseException e) {
            e.printStackTrace();
        }*/
        //Collections.reverse(result);
        return result;
    }
    /**
     * 获取两个日期之间的所有日期
     * @param start_date
     * @param end_date
     * @return
     */
    public static List<String> getBetweenYearMonthDatesYMD(Date start_date, Date end_date) {
        List<String> result = new ArrayList<String>();
        /*  try {*/
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
           /* Date start_date = sdf.parse(start);
            Date end_date = sdf.parse(end);*/
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start_date);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end_date);
        String month = "";
        while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
            if((tempStart.get(Calendar.MONTH)+1)<10){
                month = "0"+(tempStart.get(Calendar.MONTH)+1);
            }else {
                month = ""+(tempStart.get(Calendar.MONTH)+1);
            }
            if(tempStart.get(Calendar.DATE)<10){
                result.add(tempStart.get(Calendar.YEAR)+"-"+month+"-"+"0"+tempStart.get(Calendar.DATE));
            }else{
                result.add(tempStart.get(Calendar.YEAR)+"-"+month+"-"+tempStart.get(Calendar.DATE));
            }
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
       /* } catch (ParseException e) {
            e.printStackTrace();
        }*/
        //Collections.reverse(result);
        return result;
    }
    /**
     * 获取两个日期之间的所有小时
     * @param start_date
     * @param end_date
     * @return
     */
    public static List<String> getBetweenHours(Date start_date, Date end_date) {

        List<String> result = new ArrayList<String>();

        /*  try {*/

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

           /* Date start_date = sdf.parse(start);

            Date end_date = sdf.parse(end);*/

        Calendar tempStart = Calendar.getInstance();

        tempStart.setTime(start_date);

        Calendar tempEnd = Calendar.getInstance();

        tempEnd.setTime(end_date);

        while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
            if(tempStart.get(Calendar.HOUR_OF_DAY)<10)
                result.add("0"+tempStart.get(Calendar.HOUR_OF_DAY));
            else
                result.add(""+tempStart.get(Calendar.HOUR_OF_DAY));
            tempStart.add(Calendar.HOUR_OF_DAY, 1);

        }
       /* } catch (ParseException e) {

            e.printStackTrace();

        }*/

        //Collections.reverse(result);
        return result;
    }
    public static List<String> getBetweenHoursM(Date start_date, Date end_date) {

        List<String> result = new ArrayList<String>();

        /*  try {*/

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

           /* Date start_date = sdf.parse(start);

            Date end_date = sdf.parse(end);*/

        Calendar tempStart = Calendar.getInstance();

        tempStart.setTime(start_date);

        Calendar tempEnd = Calendar.getInstance();

        tempEnd.setTime(end_date);


            while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
                if(tempStart.get(Calendar.HOUR_OF_DAY)<10)
                    result.add("0"+tempStart.get(Calendar.HOUR_OF_DAY)+":00");
                else
                    result.add(""+tempStart.get(Calendar.HOUR_OF_DAY)+":00");
                tempStart.add(Calendar.HOUR_OF_DAY, 1);

        }

       /* } catch (ParseException e) {

            e.printStackTrace();

        }*/

        //Collections.reverse(result);
        return result;
    }
    /**
     * 获取两个日期之间的所有月份
     * @param start_date
     * @param end_date
     * @return
     */
    public static List<String> getBetweenMonths(Date start_date, Date end_date) {

        List<String> result = new ArrayList<String>();

        /*  try {*/

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

           /* Date start_date = sdf.parse(start);

            Date end_date = sdf.parse(end);*/

        Calendar tempStart = Calendar.getInstance();

        tempStart.setTime(start_date);

        Calendar tempEnd = Calendar.getInstance();

        tempEnd.setTime(end_date);

        while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
            int i = tempStart.get(Calendar.MONTH) +1;
            int year = tempStart.get(Calendar.YEAR);
            if(i<10)
                result.add(year + "-0" + i);
            else
                result.add(year + "-" + i);
            tempStart.add(Calendar.MONTH, 1);

        }

       /* } catch (ParseException e) {

            e.printStackTrace();

        }*/

        //Collections.reverse(result);
        return result;
    }
    public static List<String> getStrBetweenMonths(String start, String end) {

        List<String> result = new ArrayList<String>();

         try {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Date start_date = sdf.parse(start);

            Date end_date = sdf.parse(end);

        Calendar tempStart = Calendar.getInstance();

        tempStart.setTime(start_date);

        Calendar tempEnd = Calendar.getInstance();

        tempEnd.setTime(end_date);

        while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
            int i = tempStart.get(Calendar.MONTH) +1;
            if(i<10)
                result.add("0"+i);
            else
                result.add(""+i);
            tempStart.add(Calendar.MONTH, 1);

        }

        } catch (ParseException e) {

            e.printStackTrace();

        }

        //Collections.reverse(result);
        return result;
    }

    /**
     * 取当前时间往回${weeks}周的日期范围列表(01-06~01-12,01-13~01-14...升序)
     * @param weeks
     * @return
     */
    public static List<String> getDateWeeks(int weeks) {
        //日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        String beginDate,endDate;
        List<String> result = new ArrayList<>();
        for(int i = weeks - 1; i >=0 ; i-- ){
            Calendar calendar1 = Calendar.getInstance();
            Calendar calendar2 = Calendar.getInstance();
            int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
            //偏移量
            int offset1 = 1 - dayOfWeek;
            int offset2 = 7 - dayOfWeek;
            calendar1.add(Calendar.DATE, offset1 - 7 * i);
            calendar2.add(Calendar.DATE, offset2 - 7 * i);
            beginDate = sdf.format(calendar1.getTime());
            endDate = sdf.format(calendar2.getTime());
            if(i == 0){
                endDate = sdf.format(new Date());
            }
            result.add(beginDate + "~" + endDate);
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(getBetweenHours(addDateHours(new Date(),-11),new Date()));
       /* List<String> dateWeeks = getDateWeeks(7);
        for(String str : dateWeeks){
            System.out.println(str);
        }*/
    }
    /**
     * 将日期字符串按指定格式转为Date日期类型
     * @param pattern 日期格式
     * @param dateString 日期字符串(必须与格式 yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd 或 HH:mm:ss 对应)
     * @return
     * @throws ParseException
     */
    public static Date parse(String pattern,String dateString)throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date d = format.parse(dateString );
        return d;
    }
    /**
     * 获取指定日期字符串的最小时间
     * @param dateStr
     * @return
     */
    public static String getStartOfDay(String dateStr){
        if(StringUtils.isEmpty(dateStr)){
            return "";
        }
        try {
            Date date = parse(DATE_PATTERN_FORMAT, dateStr);
            return getStartOfDay(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }
    /**
     * 获取指定日期Date的最小时间
     * @param date
     * @return
     */
    public static String getStartOfDay(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN_FORMAT);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        Date date1 = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        return sdf.format(date1);
    }
    /**
     * 获取指定日期字符串的最小时间
     * @param dateStr
     * @return
     */
    public static String getEndOfDay(String dateStr){
        if(StringUtils.isEmpty(dateStr)){
            return "";
        }
        try {
            Date date = parse(DATE_PATTERN_FORMAT, dateStr);
            return getEndOfDay(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }
    /**
     * 获取指定日期Date的最大时间
     * @param date
     * @return
     */
    public static String getEndOfDay(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN_FORMAT);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        Date date1 = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        return sdf.format(date1);
    }

}

  目录