JAVA 后台时间通用类

public class DateTimeUtils {
    public static String format1 = "yyyy-MM-dd HH:mm:ss";
    public static String format2 = "yyyy-MM-dd HH:mm";
    public static String format3 = "yyyy-MM-dd";
    public static String format4 = "yyyy年MM月dd日";
    public static String format5 = "yyyy年MM月";
    public static String format6 = "yyyy年MM月dd日 HH时";
    public static  String format7 = "HH:mm";
    public static  String format8 = "yyMMddHHmmss";
    public static String format9= "yyyy-MM-dd HH";
    public static String format10 = "yyyy-MM";
    public static String format11 = "yyyy";
    public static String format12= "HH";
    public static String format13= "dd";
    public static String format14= "MM";
    public static String format15= "yyyyMMdd";
    public static String format16= "yyyyMM";


    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getCurrDate() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }

    /**
     * 获取当前时间前七天的时间
     *
     * @return
     */
    public static Date getBefore7Day() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -6);
        return c.getTime();
    }

    //判断当前时间相差的天数
    public static int belongCalendar(Date beginTime) {

        int days = (int) ((new Date().getTime() - beginTime.getTime()) / (1000*3600*24));
        return days;


    }

    /**
     * 获取当前时间的凌晨
     *
     * @return
     */
    public static Date getDayFirst(Date c) {
        Calendar currentDate = Calendar.getInstance();
        currentDate.setTime(c);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        return currentDate.getTime();
    }

    /**
     * 获取当天时间的凌晨
     *
     * @return
     */
    public static Date getcurrentDayFirst(Date c) {
        Calendar currentDate = Calendar.getInstance();
        currentDate.setTime(c);
        int day = currentDate.get(Calendar.DATE);
        currentDate.set(Calendar.DATE, day);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        return currentDate.getTime();

    }

    /**
     *
     *获取传入时间时间的前一小时的时间
     *
     */
    public static String getLastHour(Date searchTime) {
        Calendar curr = Calendar.getInstance();
        curr.setTime(searchTime);
        curr.add(Calendar.HOUR_OF_DAY, -1);
        String date = getDateToStr(curr.getTime(), format9);
        return date;
    }

    /**
     * 取当月的第一天
     *
     * @return
     */
    public static Date getMonthFirst(Date c) {
        Calendar currentDate = Calendar.getInstance();
        currentDate.setTime(c);
        currentDate.set(Calendar.DAY_OF_MONTH, 1);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        Date date = currentDate.getTime();
        return date;
    }

    /**
     * 取当年的第一个月
     *
     * @return
     */
    public static Date getYearFirst(Date c) {
        Calendar currentDate = Calendar.getInstance();
        currentDate.setTime(c);
        currentDate.set(Calendar.MONTH, 0);
        currentDate.set(Calendar.DAY_OF_MONTH, 1);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);

        return currentDate.getTime();
    }

    /**
     * 获取当前时间的毫秒数
     *
     * @return
     */
    public static long getTimeInMillis() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当前时间的后一天时间
     *
     * @param cl
     * @return
     */
    public static Date getAfterDay(Date cl) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(cl);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取当前时间的后一月时间
     *
     * @param cl
     * @return
     */
    public static Date getAfterMonth(Date cl) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(cl);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取当前时间的后一天时间
     *
     * @param cl
     * @return
     */
    public static Date getAfterYear(Date cl) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(cl);
        calendar.set(Calendar.MONTH, 12);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取月份起始日期
     * @param date
     * @return
     */
    public static String getMinMonthDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

        return getDateToStr(calendar.getTime(), format3);
    }

    /**
     * 获取月份最后日期
     * @param date
     * @return
     */
    public static String getMaxMonthDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

        return getDateToStr(calendar.getTime(), format3);
    }

    /**
     * 日期转换为字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String getDateToStr(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static List<String> getMonthList(Date beginDate, Date endDate) {
        List<String> list = new ArrayList<String>();

        Calendar calendar = Calendar.getInstance();
        long tempTime = beginDate.getTime();
        long endTime = endDate.getTime();
        while (tempTime <= endTime) {
            calendar.setTimeInMillis(tempTime);
            list.add(getDateToStr(calendar.getTime(), format14));

            calendar.add(Calendar.MONTH, 1);
            tempTime = calendar.getTimeInMillis();
        }

        return list;
    }

    /**
     * 获取时间段内的年份
     * @param beginDate
     * @param endDate
     * @return
     */
    public static List<String> getYearList(Date beginDate, Date endDate) {
        List<String> list = new ArrayList<String>();

        Calendar calendar = Calendar.getInstance();
        long tempTime = beginDate.getTime();
        long endTime = endDate.getTime();
        while (tempTime <= endTime) {
            calendar.setTimeInMillis(tempTime);
            list.add(getDateToStr(calendar.getTime(), format11));

            calendar.add(Calendar.YEAR, 1);
            tempTime = calendar.getTimeInMillis();
        }

        return list;
    }
    public static List<String> getHourList(Date beginDate, Date endDate) {
        List<String> list = new ArrayList<String>();

        Calendar calendar = Calendar.getInstance();
        long tempTime = beginDate.getTime();
        long endTime = endDate.getTime();
        while (tempTime <= endTime) {
            calendar.setTimeInMillis(tempTime);
            list.add(getDateToStr(calendar.getTime(), format12));

            calendar.add(Calendar.HOUR_OF_DAY, 1);
            tempTime = calendar.getTimeInMillis();
        }

        return list;
    }

    public static List<String> getDayList(Date beginDate, Date endDate) {
        List<String> list = new ArrayList<String>();

        Calendar calendar = Calendar.getInstance();
        long tempTime = beginDate.getTime();
        long endTime = endDate.getTime();
        while (tempTime <= endTime) {
            calendar.setTimeInMillis(tempTime);
            list.add(getDateToStr(calendar.getTime(), format13));

            calendar.add(Calendar.DATE, 1);
            tempTime = calendar.getTimeInMillis();
        }

        return list;
    }

    /**
     *
     *获取系统当前时间的前一天时间
     *
     */
    public static String getNextDay() {
        Date dNow = new Date();   //当前时间
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(dNow); //把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        String date = getDateToStr(calendar.getTime(), format3);

        return date;
    }

    /**
     *
     *获取所传时间的前一天时间
     *
     */
    public static String getLastDay(Date searchTime) {
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(searchTime); //把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
        String date = getDateToStr(calendar.getTime(), format3);

        return date;
    }
    /**
     *
     *获取传入时间的上一个月时间
     *
     */
    public static String getLastMonth(Date searchTime) {
        Calendar c = Calendar.getInstance();
        c.setTime(searchTime);
        c.add(Calendar.MONTH, -1);
        String date = getDateToStr(c.getTime(), format10);
        return date;
    }
    /**
     *
     *获取传入时间时间的前一年时间
     *
     */
    public static String getLastYear(Date searchTime) {
        Calendar curr = Calendar.getInstance();
        curr.setTime(searchTime);
        curr.add(Calendar.YEAR, -1);
        String date = getDateToStr(curr.getTime(), format11);
        return date;
    }


    /**
     *
     *字符串日期转Date类型
     *
     */
    public static Date getDateTime(String dateTime,String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date= null;
        try {
            date = sdf.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }



    /**
     * 获取两个时间中的最大时间
     *
     * @param d1
     * @param d2
     * @return
     */
    public static Date getMaxDate(Date d1, Date d2) {
        return (d1.getTime() > d2.getTime()) ? d1 : d2;
    }

    /**
     *
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static Date getDate(int year, int month, int day, int hour, int minute, int second){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);

        return calendar.getTime();
    }

    /**
     *
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static Date getMo5Date(int year, int month, int day, int hour, int minute, int second){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute - minute%5);
        calendar.set(Calendar.SECOND, second);

        return calendar.getTime();
    }

    public static String formatTo5min(Date date, Integer interval){
        if(interval == null){
            interval = 5;   //默认间隔为5分钟
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int minute = calendar.get(Calendar.MINUTE);
        calendar.add(Calendar.MINUTE, -minute%interval);

        SimpleDateFormat format = new SimpleDateFormat(format7);

        return format.format(calendar.getTime());
    }

    public static String formatTo5min(Date date, String formatStr, Integer interval){
        if(interval == null){
            interval = 5;   //默认间隔为5分钟
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int minute = calendar.get(Calendar.MINUTE);
        calendar.add(Calendar.MINUTE, -minute%interval);

        SimpleDateFormat format = new SimpleDateFormat(formatStr);

        return format.format(calendar.getTime());
    }

    /**
     * 判断两个时间相差几个月
     * @param str1
     * @return
     */
    public static int getMonth(String str1){
        SimpleDateFormat sdf = new SimpleDateFormat(format10);
//        String str1 = "2012-02";
        String str2 = getDateToStr(new Date(),format10);
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        try{
            bef.setTime(sdf.parse(str1));
            aft.setTime(sdf.parse(str2));
        }catch (Exception e){
            e.getStackTrace();
        }
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }

    /**
     * 6      * 时间戳转换成日期格式字符串
     * 7      * @param seconds 精确到秒的字符串
     * 8      * @param formatStr
     * 9      * @return
     * 10
     */
    public static Date timeStamp2Date(String seconds, String format) {
        if (format == null || format.isEmpty()) {
            format = DateUtils.format2;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return  new Date(Long.valueOf(seconds));
    }
}
发布了25 篇原创文章 · 获赞 14 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_34650238/article/details/80908893
今日推荐