JAVA DateUtils

package *.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DateUtils {

    /**
     * (ForExample)当前时间:2019-07-01 20:51:05
     *
     * @return
     */
    public static String getTimeStr2() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr2 = sf.format(date);
        return timeStr2.substring(0, 10);
    }

    /**
     * 获取年
     * @return
     */
    public static String getDateYear(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr2 = sf.format(date);
        return timeStr2.substring(0, 4);
    }

    /**
     * 获取月
     * @param date
     * @return
     */
    public static String getDateMonth(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr2 = sf.format(date);
        return timeStr2.substring(5, 7);
    }

    /**
     * 获取天
     * @param date
     * @return
     */
    public static String getDateDay(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr2 = sf.format(date);
        return timeStr2.substring(8, 10);
    }

    /**
     * 获取当天开始时间
     *
     * @return
     */
    public static Date getDayBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);//0点
        cal.set(Calendar.MINUTE, 0);//0分
        cal.set(Calendar.SECOND, 0);//0秒
        cal.set(Calendar.MILLISECOND, 0);//0毫秒
        return cal.getTime();
    }

    /**
     * 获取当天结束时间
     *
     * @return
     */
    public static Date getDayEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);//23点
        cal.set(Calendar.MINUTE, 59);//59分
        cal.set(Calendar.SECOND, 59);//59秒
        return cal.getTime();
    }

    /**
     * 获取昨天结束时间
     *
     * @return
     */
    public static Date getEndDayOfYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());//当天结束时间
        cal.add(Calendar.DAY_OF_MONTH, -1);//当天月份天数减1
        return cal.getTime();
    }


    /**
     * 获取前天结束时间
     *
     * @return
     */
    public static Date getEndDayOfYesterday2() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());//当天结束时间
        cal.add(Calendar.DAY_OF_MONTH, -2);//当天月份天数减1
        return cal.getTime();
    }


    /**
     * 获取昨天开始时间
     *
     * @return
     */
    public static Date getBeginDayOfYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());//当天开始时间
        cal.add(Calendar.DAY_OF_MONTH, -1);//当天月份天数减1
        return cal.getTime();
    }

    /**
     * 获取前天开始时间
     *
     * @return
     */
    public static Date getBeginDayOfYesterday2() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());//当天开始时间
        cal.add(Calendar.DAY_OF_MONTH, -2);//当天月份天数减1
        return cal.getTime();
    }


    /**
     * 上上月第一天
     * @return
     */
    public static Date getBeginDayOfMonthBefore2(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.MONTH, -2);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }



    /**
     * 月份+N
     * @return
     */
    public static Date getMonthInc(Date date , int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }


    /**
     * 月份+N
     * @return
     */
    public static Date getHourInc(Date date , int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);
        return cal.getTime();
    }





    /**
     * 上上月最后一日
     * @return
     */
    public static Date getBeginDayOfMonthAfter2(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayEnd());
        int month=cal.get(Calendar.MONTH);
        cal.set(Calendar.MONTH, month-2);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }




    /**
     * 下月第一天
     * @return
     */
    public static Date getBeginDayOfMonthDown(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.MONTH, +1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }


    /**
     * 下月最后一日
     * @return
     */
    public static Date getEndDayOfMonthDown(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayEnd());
        int month=cal.get(Calendar.MONTH);
        cal.set(Calendar.MONTH, month+1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }




    /**
     * 上月第一天
     * @return
     */
    public static Date getBeginDayOfMonthBefore(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }


    /**
     * 上月最后一日
     * @return
     */
    public static Date getBeginDayOfMonthAfter(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayEnd());
        int month=cal.get(Calendar.MONTH);
        cal.set(Calendar.MONTH, month-1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }


    /**
     * 当月第一天
     * @return
     */
    public static Date getBeginDayOfMonth(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 当月最后一日
     * @return
     */
    public static Date getEndDayOfMonth(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return cal.getTime();
    }

    /**
     * date类型进行格式化输出(返回类型:String)
     *
     * @param date
     * @return
     */
    public static String dateFormat(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 获取当前时间到第二天的秒数
     *
     * @return
     */
    public static Long getSecondsNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

    /**
     * 获取本周第一天
     * @return
     */
    public static Date getBeginDayOfWeek(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        return cal.getTime();
    }


    /**
     * 获取上周第一天
     * @return
     */
    public static Date getBeginDayOfWeekTop(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.set(Calendar.DAY_OF_WEEK,1);
        cal.add(Calendar.DATE, -6);
        return cal.getTime();
    }



    /**
     * 获取上上周第一天
     * @return
     */
    public static Date getBeginDayOfWeek2Top(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.set(Calendar.DAY_OF_WEEK,1);
        cal.add(Calendar.DATE, -13);
        return cal.getTime();
    }



    /**
     * 获取本周最后一日
     * @return
     */
    public static Date getEndDayOfWeek(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        return cal.getTime();
    }

    /**
     * 获取上周最后一日
     * @return
     */
    public static Date getEndDayOfWeekTop(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.set(Calendar.DAY_OF_WEEK,1);
        cal.add(Calendar.DATE, 0);
        return cal.getTime();
    }


    /**
     * 本年第一天
     * @return
     */
    public static Date getBeginDayOfYear(){
        //Date now = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        //cal.setTime(now);
        cal.set(Calendar.DAY_OF_YEAR, 1);
        return cal.getTime();
    }

    /**
     * 下一年第一天
     * @return
     */
    public static Date getBeginDayOfNextYear(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.YEAR, 1);
        cal.set(Calendar.DAY_OF_YEAR, 1);
        return cal.getTime();
    }



    /**
     * 本年最后一日
     * @return
     */
    public static Date getEndDayOfYear(){
        Calendar cal=Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.YEAR, 1);
        cal.set(Calendar.DAY_OF_YEAR, -1);
        return cal.getTime();
    }


    /**
     * 获取当天前7天时间
     *
     * @return
     */
    public static Date getDayBeforeSeven() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -7);//当天月份天数减1
        return cal.getTime();
    }

    /**
     * 天数+N
     *
     * @return
     */
    public static Date getDayInc(Date date , int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, day);
        return c.getTime();
    }


    /**
     * 两个日期间 相差月份
     * @param date1
     * @param date2
     * @return
     */
    public static int getMonthSpace(String date1, String date2){
        try {
            int result = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));
            result =  c2.get(Calendar.HOUR) - c1.get(Calendar.MONTH) + ( (c2.get(Calendar.YEAR)  - c1.get(Calendar.YEAR)) * 12  ) ;
            return result == 0 ? 1 : Math.abs(result);
        }catch (Exception e) {
            return  0;
        }
    }

    /**
     * 两个日期间 相差小时
     * @param date1
     * @param date2
     * @return
     */
    public static int getHourSpace(String date1, String date2){
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            int result = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));
            result =  c2.get(Calendar.HOUR) - c1.get(Calendar.HOUR) + ( ((int) ((simpleDateFormat.parse(date1).getTime() - simpleDateFormat.parse(date2).getTime()) / (1000*3600*24))) * 24   ) ;
            return result == 0 ? 1 : Math.abs(result);
        }catch (Exception e) {
            return  0;
        }
    }


    public static String dateToString(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }


    /**
     * 自定义时间格式
     * @param date
     * @param param
     * @return
     */
    public static String dateToStringSet(Date date , String param) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(param);
        return dateFormat.format(date);
    }


    /**
     * 2个时间相差天数
     * @param s
     * @param e
     * @return
     */
    public static Integer dateDiff(String s , String e ) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate   =  sdf.parse(s);
            Date endDate     =  sdf.parse(e);
            return ((int)(endDate.getTime()/1000)-(int)(startDate.getTime()/1000))/3600/24;
        }catch (Exception e1) {
            log.info("dateDiff-error:"+e1.getMessage());
            return 0;
        }
    }


    /**
     * 字符串转时间
     * @param s
     * @return
     */
    public static Date stringToDate(String s) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = simpleDateFormat.parse(s);
            return date;
        }catch (Exception e) {
            return null;
        }
    }

    /**
     * date 转 localDateTime
     * @return
     */
    public static LocalDateTime dateToLocalDateTime( Date date) {
        return date.toInstant().atZone( ZoneId.systemDefault() ).toLocalDateTime();
    }






}
上一篇:算法笔记-之字形打印数组


下一篇:Java RocketMQ3:顺序消息