Java获取指定格式日期、时间工具类

package com.crm.dubbo.util;

import java.math.BigDecimal;
/** 
 * ClassName:DateUtils 
 * Date:     2019年7月27日 上午11:12:29
 * @author   ZHUJIANYI  
 * @since    JDK 1.8.0_102    
 */
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtils {

    private static final SimpleDateFormat SDFS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");

    // 获取当天的开始时间
    public static java.util.Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获取当天的结束时间
    public static java.util.Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    // 获取昨天的开始时间
    public static Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    // 获取昨天的结束时间
    public static Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    // 获取明天的开始时间
    public static Date getBeginDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);

        return cal.getTime();
    }

    // 获取明天的结束时间
    public static Date getEndDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    // 获取本周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    // 获取本周的结束时间
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    // 获取上周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }

    // 获取上周的结束时间
    public static Date getEndDayOfLastWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    // 获取本月的开始时间
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    // 获取本月的结束时间
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }

    // 获取上月的开始时间
    public static Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        return getDayStartTime(calendar.getTime());
    }

    // 获取上月的结束时间
    public static Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 2, day);
        return getDayEndTime(calendar.getTime());
    }

    // 获取本年的开始时间
    public static java.util.Date getBeginDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        // cal.set
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);

        return getDayStartTime(cal.getTime());
    }

    // 获取本年的结束时间
    public static java.util.Date getEndDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getNowYear());
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }

    // 获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
                0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
                59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    // 获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    // 两个日期相减得到的天数
    public static int getDiffDays(Date beginDate, Date endDate) {

        if (beginDate == null || endDate == null) {
            throw new IllegalArgumentException("getDiffDays param is null!");
        }

        long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);

        int days = new Long(diff).intValue();

        return days;
    }

    // 两个日期相减得到的毫秒数
    public static long dateDiff(Date beginDate, Date endDate) {
        long date1ms = beginDate.getTime();
        long date2ms = endDate.getTime();
        return date2ms - date1ms;
    }

    // 获取两个日期中的最大日期
    public static Date max(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return beginDate;
        }
        return endDate;
    }

    // 获取两个日期中的最小日期
    public static Date min(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return endDate;
        }
        return beginDate;
    }

    // 返回某月该季度的第一个月
    public static Date getFirstSeasonDate(Date date) {
        final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int sean = SEASON[cal.get(Calendar.MONTH)];
        cal.set(Calendar.MONTH, sean * 3 - 3);
        return cal.getTime();
    }

    // 返回某个日期下几天的日期
    public static Date getNextDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
        return cal.getTime();
    }

    // 返回某个日期前几天的日期
    public static Date getFrontDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
        return cal.getTime();
    }

    // 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) {
        List list = new ArrayList();
        if (beginYear == endYear) {
            for (int j = beginMonth; j <= endMonth; j++) {
                list.add(getTimeList(beginYear, j, k));

            }
        } else {
            {
                for (int j = beginMonth; j < 12; j++) {
                    list.add(getTimeList(beginYear, j, k));
                }

                for (int i = beginYear + 1; i < endYear; i++) {
                    for (int j = 0; j < 12; j++) {
                        list.add(getTimeList(i, j, k));
                    }
                }
                for (int j = 0; j <= endMonth; j++) {
                    list.add(getTimeList(endYear, j, k));
                }
            }
        }
        return list;
    }

    // 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static List getTimeList(int beginYear, int beginMonth, int k) {
        List list = new ArrayList();
        Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
        int max = begincal.getActualMaximum(Calendar.DATE);
        for (int i = 1; i < max; i = i + k) {
            list.add(begincal.getTime());
            begincal.add(Calendar.DATE, k);
        }
        begincal = new GregorianCalendar(beginYear, beginMonth, max);
        list.add(begincal.getTime());
        return list;
    }

    // 获取指定几天前的日期 yyyy-MM-dd
    public static String getDateByNumber(int num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -num);
        Date time = c.getTime();
        String preDay = sdf.format(time);
        return preDay;
    }
    
    // 获取指定几天后的日期 yyyy-MM-dd
    public static String getDateByNumberLater(int num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -num);
        Date time = c.getTime();
        String preDay = sdf.format(time);
        return preDay;
    }

    /**
     * 获取指定日期所在周的周一的时间
     * 
     * @author ZHUJIANYI
     * @param date yyyy-MM-dd
     * @return yyyy-MM-dd 00:00:00
     */
    public static Date getFirstDayOfWeek(String date) {
        try {
            Date date2 = SDF.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(date2);
            if (c.get(Calendar.DAY_OF_WEEK) == 1) {
                c.add(Calendar.DAY_OF_MONTH, -1);
            }
            c.add(Calendar.DATE, c.getFirstDayOfWeek() - c.get(Calendar.DAY_OF_WEEK) + 1);
            return c.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定日期所在周的周日的时间
     * 
     * @author ZHUJIANYI
     * @param date yyyy-MM-dd
     * @return yyyy-MM-dd 00:00:00
     */
    public static Date getLastDayOfWeek(String date) {
        try {
            Date date2 = SDF.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(date2);
            // 如果是周日直接返回
            if (c.get(Calendar.DAY_OF_WEEK) == 1) {
                date = date + " 00:00:00";
                Date parse = SDFS.parse(date);
                return parse;
            }
//            System.out.println(c.get(Calendar.DAY_OF_WEEK));
            c.add(Calendar.DATE, 7 - c.get(Calendar.DAY_OF_WEEK) + 1);
            return c.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取传入日期所在月的第一天
     * 
     * @author ZHUJIANYI
     * @param date
     * @return
     */
    public static Date getFirstDayDateOfMonth(String date) {
        try {
            Date dates = SDFS.parse(date);
            Calendar cal = Calendar.getInstance();
            cal.setTime(dates);
            int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, last);
            return cal.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取传入日期所在月的最后一天
     * 
     * @author ZHUJIANYI
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(String date) {
        try {
            Date dates = SDFS.parse(date);
            Calendar cal = Calendar.getInstance();
            cal.setTime(dates);
            int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, last);
            return cal.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取传入日期所在年的第一天
     * 
     * @author ZHUJIANYI
     * @param date
     * @return
     */
    public static Date getFirstDayDateOfYear(final Date date) {

        final Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        final int last = cal.getActualMinimum(Calendar.DAY_OF_YEAR);

        cal.set(Calendar.DAY_OF_YEAR, last);

        return cal.getTime();

    }

    /**
     * 获取传入日期所在年的最后一天
     * 
     * @author ZHUJIANYI
     * @param date
     * @return
     */
    public static Date getLastDayOfYear(final Date date) {

        final Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        final int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);

        cal.set(Calendar.DAY_OF_YEAR, last);

        return cal.getTime();

    }
    /**
     * 指定日期的上月最后一天
     * @author ZHUJIANYI
     * @param date yyyy-MM-dd HH:mm:ss
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getLastDayOfLastMonth(String date) {
        try {
            Date dates = SDFS.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(dates);
            c.add(Calendar.MONTH, -1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
            // 按格式输出
            String gtime = sdf.format(c.getTime()); // 上月最后一天
            return gtime;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取指定日期上月的第一天
     * @author ZHUJIANYI
     * @param date yyyy-MM-dd HH:mm:ss
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getFirstDayOfLastMonth(String date) {
        try {
            Date dates = SDFS.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(dates);
            c.add(Calendar.MONTH, -1);
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01  00:00:00");
            String gtime2 = sdf2.format(c.getTime()); // 上月第一天
            System.out.println(gtime2);
            return gtime2;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取指定日期当周的上一周的结束时间(上周日)
     * @author ZHUJIANYI
     * @param date
     * @return
     */
    public static String getLastDayOfLasWeek(String date) {
        Date fweek = getFirstDayOfWeek(date);
        Calendar c = Calendar.getInstance();
        c.setTime(fweek);
        c.add(Calendar.DATE, -1);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        String format = sdf2.format(c.getTime());
        return format;
    }
    /**
     * 获取指定日期当周的上一周的开始时间(上周一)
     * @author ZHUJIANYI
     * @param date
     * @return
     */
    public static String getFirstDayOfLasWeek(String date) {
        Date fweek = getFirstDayOfWeek(date);
        Calendar c = Calendar.getInstance();
        c.setTime(fweek);
        c.add(Calendar.DATE, -7);
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String format = sdf3.format(c.getTime());
        return format;
    }
    
    /**
     * 获取指定日期前一天的开始时间
     * @author ZHUJIANYI
     * @param date
     * @return
     */
    public static String getBeginTimeOfLastDay(String date) {
        try {
            Date parse = SDFS.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.add(Calendar.DATE, -1);
            SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            String format = sdf3.format(c.getTime());
            return format;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取指定日期前一天的结束时间
     * @author ZHUJIANYI
     * @param date
     * @return
     */
    public static String getEndTimeOfLastDay(String date) {
        try {
            Date parse = SDFS.parse(date);
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.add(Calendar.DATE, -1);
            SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
            String format = sdf3.format(c.getTime());
            return format;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 计算两个时间相差天数:如2.26
     * @author ZHUJIANYI
     * @param startDate
     * @param endDate2
     * @return
     */
    public static double getDiffIn_2_times(String startDate, String endDate) {
        try {
            Date startDate1 = SDFS.parse(startDate);
            Date endDate2 = SDFS.parse(endDate);
            long nd = 1000 * 24 * 60 * 60;
            long nh = 1000 * 60 * 60;
            long nm = 1000 * 60;
            // 获得两个时间的毫秒时间差异
            long diff = endDate2.getTime() - startDate1.getTime();
            // 计算差多少天
            long day = diff / nd;
            // 计算差多少小时
            long hour = diff % nd / nh;
            // 计算差多少分钟
            long min = diff % nd % nh / nm;
//            System.out.println(day + "天" + hour + "小时" + min + "分钟");
            BigDecimal day1 = new BigDecimal(day);
            BigDecimal hour1 = new BigDecimal(hour);
            BigDecimal min1 = new BigDecimal(min);
            BigDecimal div1 = min1.divide(new BigDecimal(60), 1, BigDecimal.ROUND_HALF_UP);
            BigDecimal add1 = hour1.add(div1);
            BigDecimal div2 = add1.divide(new BigDecimal(24), 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal add2 = day1.add(div2);
            return add2.doubleValue();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0.0;
    }
    /**
     * 获取指定日期的 前/后?天
     * @author ZHUJIANYI
     * @param d
     * @param day
     * @return
     */
    public static Date rollDay(Date d, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }
    /**
     * 获取指定日期的 前/后?月
     * 
     * @param d
     * @param mon
     * @return
     */
    public static Date rollMon(Date d, int mon) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.MONTH, mon);
        return cal.getTime();
    }
    
    /**
     * 获取指定日期的 前/后?年
     * 
     * @param d
     * @param year
     * @return
     */
    public static Date rollYear(Date d, int year) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.YEAR, year);
        return cal.getTime();
    }
    /**
     * 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串)
     * @author ZHUJIANYI
     * @param year
     * @param month
     * @return
     */
    public static List<String> getMonthFullDay(int year , int month){
        SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList<>(32);
        // 获得当前日期对象
        Calendar cal = Calendar.getInstance();
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        // 1月从0开始
        cal.set(Calendar.MONTH, month-1 );
        // 当月1号
        cal.set(Calendar.DAY_OF_MONTH,1);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 1; j <= count ; j++) {
            fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
            cal.add(Calendar.DAY_OF_MONTH,1);
        }
        return fullDayList;
    }
    
    /**
     * JAVA获取某段时间内的所有日期
     * @author ZHUJIANYI
     * @param dStart
     * @param dEnd
     * @return
     */
    public static List<Date> findDates(Date dStart, Date dEnd) {
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(dStart);

        List dateList = new ArrayList();
        //别忘了,把起始日期加上
        dateList.add(dStart);
        // 此日期是否在指定日期之后
        while (dEnd.after(cStart.getTime())) {
        // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            cStart.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(cStart.getTime());
        }
        return dateList;
    }
    
    
    public static void main(String[] args) throws ParseException {
        
//        String date = "2019-11-25 13:23:43";
//        String endTimeOfLastDay = getEndTimeOfLastDay(date);
//        String beginTimeOfLastDay = getBeginTimeOfLastDay(date);
//        System.out.println(endTimeOfLastDay);
//        System.out.println(beginTimeOfLastDay);
        
//        String date = "2019-11-05 12:43:43";
//        Date fweek = getFirstDayOfWeek(date);
//        Calendar c = Calendar.getInstance();
//        c.setTime(fweek);
        c.add(Calendar.DATE, -1);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        String format = sdf2.format(c.getTime());
        System.out.println(format);
//        c.add(Calendar.DATE, -7);
//        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
//        String format = sdf3.format(c.getTime());
//        System.out.println(format);
        
//        Calendar c = Calendar.getInstance();
//        Date parse = SDFS.parse("2019-01-12 12:23:41");
//        c.setTime(parse);
//        c.add(Calendar.MONTH, -1);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
//        int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
//        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
//
//        // 按格式输出
//        String gtime = sdf.format(c.getTime()); // 上月最后一天
//        System.out.println(gtime);
//        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01  00:00:00");
//        String gtime2 = sdf2.format(c.getTime()); // 上月第一天
//        System.out.println(gtime2);

        // 当前时分秒
//        Date date = new Date();
//        String now = SDFS.format(date);
//        String[] split = now.split(" ");
//        String time = split[1];
//        // 今日
//        Date beginDayOfYesterday = DateUtils.getBeginDayOfYesterday();
//        String yesterday = SDF.format(beginDayOfYesterday);
//        String startDateDay1 = SDFS.format(beginDayOfYesterday);
//        String endDateDay1 = yesterday+" "+time;
//        String startDateDay2 = split[0]+" "+"00:00:00";
//        String endDateDay2 = now;
//        // 本周
//        Date beginDayOfWeek = DateUtils.getBeginDayOfWeek();
//        String startDateWeek1 = SDFS.format(beginDayOfWeek);
//        String endDateWeek1 = now;
//        Date beginDayOfLastWeek = DateUtils.getBeginDayOfLastWeek();
//        String startDateWeek2 = SDFS.format(beginDayOfLastWeek);
//        // 周相差天数
//        int num = DateUtils.getDiffDays(beginDayOfWeek, date);
//            Calendar c = Calendar.getInstance();
//          c.setTimeInMillis(beginDayOfLastWeek.getTime());
//          // num天后的日期
//          c.add(Calendar.DATE, num);
//          Date dates = new Date(c.getTimeInMillis());
//          String endDateWeek2 = SDF.format(dates)+" " + time;
//          System.out.println(dates);
//        // 本月
//          Date beginDayOfMonth = DateUtils.getBeginDayOfMonth();
//          String startDateMonth1 = SDFS.format(beginDayOfMonth);
//          String endDateMonth1 = now;
//          Date beginDayOfLastMonth = DateUtils.getBeginDayOfLastMonth();
//          Date endDayOfLastMonth = DateUtils.getEndDayOfLastMonth();
//          int n1 = DateUtils.getDiffDays(beginDayOfLastMonth, endDayOfLastMonth);
//        String startDateMonth2 = SDFS.format(beginDayOfLastMonth);
//        int n2 = DateUtils.getDiffDays(beginDayOfMonth, date);
//        String endDateMonth2 = null;
//        if(n2 <= n1) {
//            c.setTimeInMillis(beginDayOfMonth.getTime());
//              // num天后的日期
//              c.add(Calendar.DATE, num);
//              Date dates2 = new Date(c.getTimeInMillis());
//              endDateMonth2 = SDF.format(dates2) +" "+time;
//        }else{
//            endDateMonth2 = SDFS.format(endDayOfLastMonth);
//        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值