前后端时间戳和时间的转换

落花浮王杯 提交于 2019-12-01 23:35:28
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

public class MyDateUtils {
    
    public static final String DATE_FORMAT_YYYYMM1 = "yyyyMM";
    public static final String DATE_FORMAT_YYYYMMDD1 = "yyyyMMdd";
    public static final String DATE_FORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";

    /**
     * 获取任意时间的月的最后一天
     * 描述:<描述函数实现的功能>.
     * @param repeatDate
     * @return
     */
    public static Date getMaxMonthDate(long repeatDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(repeatDate));
        // calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
        return calendar.getTime();
    }
    
    /**
     * 获取任意时间的月第一天
     * 描述:<描述函数实现的功能>.
     * @param repeatDate
     * @return
     */
    public static Date getMinMonthDate(long repeatDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(repeatDate));
        // calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));
        return calendar.getTime();
    }
    
    public static Date getMaxDayDate(long repeatDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(repeatDate));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
        return calendar.getTime();
    }
    
    public static Date getMinDayDate(long repeatDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(repeatDate));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));
        return calendar.getTime();
    }
    
    public static Date getMinYearDate(long repeatDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(repeatDate));
        calendar.set(Calendar.MONTH, calendar.getActualMinimum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));
        return calendar.getTime();
    }
    
    public static Date getMaxYearDate(long repeatDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(repeatDate));
        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
        return calendar.getTime();
    }
    
    /**
     * 获取当前时间
     */
    public static String getNowTime() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }
    
    /**
     * 判断当天是否为本月第一天
     * @return
     */
    public static boolean isFirstDayOfMonth() {
        boolean flag = false;
        Calendar calendar = Calendar.getInstance();
        int today = calendar.get(Calendar.DAY_OF_MONTH);
        if (1 == today) {
            flag = true;
        }
        return flag;
    }

    /**
     * 判断当天是否为本月第二天
     * @return
     */
    public static boolean isSencondDayOfMonth() {
        boolean flag = false;
        Calendar calendar = Calendar.getInstance();
        int today = calendar.get(Calendar.DAY_OF_MONTH);
        if (2 == today) {
            flag = true;
        }
        return flag;
    }

    /**
     * 获取当前月份最后一天
     * @return
     * @throws ParseException
     */
    public static String getMaxMonthDate() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        // calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }
    
    /**
     * 描述:获取下一个月的第一天.
     * @return
     */
    public static String getPerFirstDayOfMonth() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }
    
    /**
     * 描述:获取上个月的最后一天.
     * @return
     */
    public static String getLastMaxMonthDate() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }
    
    /**
     * 获取上一个月
     * @return
     */
    public static String getLastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }
    
    /**
     * 获取上一个月
     * @return
     */
    public static Date getLastMonth(long time, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        cal.add(Calendar.MONTH, i);
        return cal.getTime();
    }
    
    /**
     * 描述:获取下一个月.
     * @return
     */
    public static String getPreMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        String preMonth = dft.format(cal.getTime());
        return preMonth;
    }
    
    /**
     * 描述:获取下一个月.
     * @return
     */
    public static Date getPreMonth(long time, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        cal.add(Calendar.MONTH, i);
        return cal.getTime();
    }
    
    /**
     * 描述:获取下一个年.
     * @return
     */
    public static Date getPreYear(long time, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        cal.add(Calendar.YEAR, i);
        return cal.getTime();
    }
    
    
    /**
     * 是否是本月最后一天
     * @return
     */
    public static boolean isLastDayOfMonth() {
        boolean flag = false;
        if (StringUtils.isNotBlank(getNowTime()) && StringUtils.isNotBlank(getMaxMonthDate()) && StringUtils
                .equals(getNowTime(), getMaxMonthDate())) { // getMaxMonthDate().equals(getNowTime())
            flag = true;
        }
        return flag;
    }
    
    /**
     * 是否是最后一天
     * @return
     */
    public static boolean isLastDayOfMonth(Date date) {
        boolean flag = false;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (day == calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) {
            flag = true;
        }
        return flag;
    }
    
    /**
     * 是否是最后一个月
     * @return
     */
    public static boolean isLastMonth(Date date) {
        boolean flag = false;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.MONTH);
        if (day == calendar.getActualMaximum(Calendar.MONTH)) {
            flag = true;
        }
        return flag;
    }
    
    /**
     * 获取任意时间的下一个月
     * 描述:<描述函数实现的功能>.
     * @param repeatDate
     * @return
     */
    public static String getPreMonth(String repeatDate) {
        String lastMonth = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        int year = Integer.parseInt(repeatDate.substring(0, 4));
        String monthsString = repeatDate.substring(4, 6);
        int month;
        if ("0".equals(monthsString.substring(0, 1))) {
            month = Integer.parseInt(monthsString.substring(1, 2));
        } else {
            month = Integer.parseInt(monthsString.substring(0, 2));
        }
        cal.set(year, month, Calendar.DATE);
        lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }
    
    /**
     * 获取任意时间的上一个月
     * 描述:<描述函数实现的功能>.
     * @param repeatDate
     * @return
     */
    public static String getLastMonth(String repeatDate) {
        String lastMonth = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        int year = Integer.parseInt(repeatDate.substring(0, 4));
        String monthsString = repeatDate.substring(4, 6);
        int month;
        if ("0".equals(monthsString.substring(0, 1))) {
            month = Integer.parseInt(monthsString.substring(1, 2));
        } else {
            month = Integer.parseInt(monthsString.substring(0, 2));
        }
        cal.set(year, month - 2, Calendar.DATE);
        lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }
    //
    
    /**
     * 不论是当前时间,还是历史时间  皆是时间点的前天
     * <p>
     * repeatDate  任意时间
     */
    public static String getModify2DaysAgo(String repeatDate) {
        Calendar cal = Calendar.getInstance();
        String daysAgo = "";
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        if (repeatDate == null || "".equals(repeatDate)) {
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 2);
            
        } else {
            int year = Integer.parseInt(repeatDate.substring(0, 4));
            String monthsString = repeatDate.substring(4, 6);
            int month;
            if ("0".equals(monthsString.substring(0, 1))) {
                month = Integer.parseInt(monthsString.substring(1, 2));
            } else {
                month = Integer.parseInt(monthsString.substring(0, 2));
            }
            String dateString = repeatDate.substring(6, 8);
            int date;
            if ("0".equals(dateString.subSequence(0, 1))) {
                date = Integer.parseInt(dateString.substring(1, 2));
            } else {
                date = Integer.parseInt(dateString.substring(0, 2));
            }
            cal.set(year, month - 1, date - 1);
            System.out.println(dft.format(cal.getTime()));
        }
        daysAgo = dft.format(cal.getTime());
        return daysAgo;
    }
    
    /**
     * 不论是当前时间,还是历史时间  皆是时间点的T-N天
     * <p>
     * repeatDate 任意时间    param 数字 可以表示前几天
     */
    public static String getModifyNumDaysAgo(String repeatDate, int param) {
        Calendar cal = Calendar.getInstance();
        String daysAgo = "";
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        if (repeatDate == null || "".equals(repeatDate)) {
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) - param);
            
        } else {
            int year = Integer.parseInt(repeatDate.substring(0, 4));
            String monthsString = repeatDate.substring(4, 6);
            int month;
            if ("0".equals(monthsString.substring(0, 1))) {
                month = Integer.parseInt(monthsString.substring(1, 2));
            } else {
                month = Integer.parseInt(monthsString.substring(0, 2));
            }
            String dateString = repeatDate.substring(6, 8);
            int date;
            if ("0".equals(dateString.subSequence(0, 1))) {
                date = Integer.parseInt(dateString.substring(1, 2));
            } else {
                date = Integer.parseInt(dateString.substring(0, 2));
            }
            cal.set(year, month - 1, date - param + 1);
            System.out.println(dft.format(cal.getTime()));
        }
        daysAgo = dft.format(cal.getTime());
        return daysAgo;
    }
    
    /**
     * 取得当前日期是多少周
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        
        return c.get(Calendar.WEEK_OF_YEAR);
    }
    
    /**
     * 得到某一年周的总数
     * @param year
     * @return
     */
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = Calendar.getInstance();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        
        return getWeekOfYear(c.getTime());
    }
    
    /**
     * 得到某年某周的第一天
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        
        return getFirstDayOfWeek(cal.getTime());
    }
    
    /**
     * 得到某年某周的最后一天
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        
        return getLastDayOfWeek(cal.getTime());
    }
    
    /**
     * 取得指定日期所在周的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }
    
    /**
     * 取得指定日期所在周的最后一天
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }
    
    /**
     * 取得当前日期所在周的第一天
     * @return
     */
    public static Date getFirstDayOfWeek() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }
    
    /**
     * 取得当前日期所在周的最后一天
     * @return
     */
    public static Date getLastDayOfWeek() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }
    
    /**
     * 获取当前时间的后一天时间
     */
    public static Date getAfterDay(long date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(date));
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + days);

        return calendar.getTime();
    }
    
    /**
     * 获取时间的前一天时间
     */
    public static Date getBeforeDay(long date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(date));
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day - days);
        return calendar.getTime();
    }
    
    public static int countTwoDate(long startDate, long endDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(startDate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(new Date(endDate));
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(betweenDays));
    }
    
    public static int until(long startTime, long endTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(startTime));
        LocalDate localDate = LocalDate.of(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
        
        cal.setTime(new Date(endTime));
        LocalDate endLocalDate = LocalDate.of(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
        return (int)localDate.until(endLocalDate, ChronoUnit.DAYS);
    }

    public static int getDayOfMonth(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        return cal.get(Calendar.DAY_OF_MONTH);
    }
    
    public static int getMonth(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        return cal.get(Calendar.MONTH);
    }

    public static int getYear(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        return cal.get(Calendar.YEAR);
    }

    public static String getYMD(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }

    public static String getYMD2(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy年MM月dd日");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }

    public static String getYMD3(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy/MM/dd");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }

    public static String getYMD4(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }

    public static String getSepDate(Date date1, Date date2) {
        String result;
        int startYear = MyDateUtils.getYear(date1.getTime());
        int endYear = MyDateUtils.getYear(date2.getTime());
        if (startYear == endYear) {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat dft = new SimpleDateFormat("yyyy.MM.dd");
            cal.setTime(date1);
            result = dft.format(cal.getTime());

            SimpleDateFormat dft2 = new SimpleDateFormat("MM.dd");
            cal.setTime(date2);
            result = result + "~" + dft2.format(cal.getTime());
        } else {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat dft = new SimpleDateFormat("yyyy.MM.dd");
            cal.setTime(date1);
            result = dft.format(cal.getTime());

            SimpleDateFormat dft2 = new SimpleDateFormat("yyyy.MM.dd");
            cal.setTime(date2);
            result = result + "~" + dft2.format(cal.getTime());
        }

        return result;
    }

    public static String getYMDHMS(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMddhhmmss");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }

    public static String getYMDHMS(long date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat(DATE_FORMAT_YYYYMMDDHHMMSS);
        cal.setTime(new Date(date));
        return dft.format(cal.getTime());
    }

    public static String getYMDHM(long date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat(DATE_FORMAT_YYYYMMDDHHMM);
        cal.setTime(new Date(date));
        return dft.format(cal.getTime());
    }


    public static String getYM(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }
    
    public static String getYM2(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy年MM月");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }

    public static String getY(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }

    public static String getY2(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy年");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }

    public static String getMD(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("MM月dd日");
        cal.setTime(date);
        return dft.format(cal.getTime());
    }
    
    public static Long getMillisYM(String date) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        try {
            Date ym = dft.parse(date);
            return ym.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static Long getMillisY(String date) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy");
        try {
            Date ym = dft.parse(date);
            return ym.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Long getMillisWithFormat(String date,String format) {
        SimpleDateFormat dft = new SimpleDateFormat(format);
        try {
            Date ym = dft.parse(date);
            return ym.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式
     * @param date2 被比较的时间  为空(null)则为当前时间
     * @param stype 返回值类型   0为多少天,1为多少个月,2为多少年
     * @return
     */
    public static int compareDate(String date1, String date2, int stype) {
        int n = 0;
        
        String[] u = {"天", "月", "年"};
        String formatStyle = stype == 1 ? DATE_FORMAT_YYYYMM1 : DATE_FORMAT_YYYYMMDD1;
        
        date2 = date2 == null ? getCurrentDate() : date2;
        
        DateFormat df = new SimpleDateFormat(formatStyle);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(date1));
            c2.setTime(df.parse(date2));
        } catch (Exception e3) {
            System.out.println("wrong occured");
        }
        while (!c1.after(c2)) {                     // 循环对比,直到相等,n 就是所要的结果
            //list.add(df.format(c1.getTime()));    // 这里可以把间隔的日期存到数组中 打印出来
            n++;
            if (stype == 1) {
                c1.add(Calendar.MONTH, 1);          // 比较月份,月份+1
            } else {
                c1.add(Calendar.DATE, 1);           // 比较天数,日期+1
            }
        }
        n = n - 1;
        if (stype == 2) {
            n = (int) n / 365;
        }
        return n + 1;
    }
    
    /**
     * 得到当前日期
     *
     * @return
     */
    public static String getCurrentDate() {
        Calendar c = Calendar.getInstance();
        Date date = c.getTime();
        SimpleDateFormat simple = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD1);
        return simple.format(date);
    }

    /**
     * 得到当前时间
     *
     * @return
     */
    public static String getCurrentTime() {
        Calendar c = Calendar.getInstance();
        Date date = c.getTime();
        SimpleDateFormat simple = new SimpleDateFormat(DATE_FORMAT_YYYYMMDDHHMMSS);
        return simple.format(date);
    }
    
    public static List<Date> getMonths(Date d1, Date d2) {
        Calendar dd = Calendar.getInstance();
        dd.setTime(getMinMonthDate(d1.getTime()));//设置日期起始时间
        List<Date> dateList = new ArrayList<>();
        while(dd.getTime().compareTo(d2) <= 0) {//判断是否到结束日期
            dateList.add(dd.getTime());
            dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
        }
        return dateList;
    }
    /**
     * 得到当前小时数
     *
     * @return
     */
    public static String getMillisHH() {
        SimpleDateFormat dft = new SimpleDateFormat("mmss");
        Date date = new Date();
        return dft.format(date);
    }

    public static String timeAAChange(String time) {
        if (StringUtils.isEmpty(time)) {
            return null;
        }
        SimpleDateFormat formatFrom = new SimpleDateFormat("hh:mm aa", Locale.ENGLISH);
        try {
            Date gmtTime = formatFrom.parse(time);
            SimpleDateFormat formatTo = new SimpleDateFormat("HH:mm");
            return formatTo.format(gmtTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    
}

先贡献一个时间工具类

剩下的内容比较简单

前台的时间格式是这样的

 

 后台处理

MyDateUtils.getMillisWithFormat(endDate,"yyyy-MM-dd HH:mm")/1000

转换后

1569146280

 

 

以上就是这次使用的场景及转换方法

 

2019-10-22  17:59:51

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!