java时间通用处理类

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author aric
 * @create 2020-05-13-14:38
 * @fun
 */
@Slf4j
public class DataFormatUtil {

    DateFormat dfByDay = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat dfByMonth = new SimpleDateFormat("yyyy-MM");
    DateFormat dfByYear = new SimpleDateFormat("yyyy");

    /**
     * @param divisor  除数
     * @param dividend 被除数
     * @param scale    精确度
     * @return 采用四舍五入的方式保留Scale位小数
     */
    public static Double keepDecimal(int divisor, int dividend, int scale) {
        BigDecimal divide1 = new BigDecimal(divisor);
        BigDecimal dividend1 = new BigDecimal(dividend);
        return dividend1.divide(divide1, scale, 4).doubleValue();
    }

    //前n天时间
    public static String CurrentYMD(int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -n);
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    //date后n天时间
    public static String AfterDateYMD(Date date, int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, +n);
        Date date1 = calendar.getTime();
        return sdf.format(date1);
    }
  //取date下一个小时数据
    public static String AfterDateHM(Date date, int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, +n);
        Date date1 = calendar.getTime();
        return sdf.format(date1);
    }

    //String 转date "yyyy-MM-dd "
    public static Date StringToDate(String dateStr) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            date = sf.parse(dateStr);
            //打印Date
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    //String 转date "yyyy-MM-dd HH:mm:ss"
    public static Date StringToDateAll(String dateStr) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            date = sf.parse(dateStr);
            //打印Date
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    //后n天时间分秒时
    public static String AfterCurrentYMDHMS(Date date, int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, +n);
        Date date1 = calendar.getTime();
        return sdf.format(date1);
    }
    //后n天时间分秒时
    public static String AfterCurrentHourS(Date date, int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, +n);
        Date date1 = calendar.getTime();
        return sdf.format(date1);
    }
   //获取当前小时 "HH"
    public static String currentHourMinute() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
        long l = System.currentTimeMillis();
        return simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(l))));
    }
    //获取当天时分秒 "yyyy-MM-dd"
    public static String currentYMDTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long l = System.currentTimeMillis();
        return simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(l))));
    }
    //获取当天时分秒 "yyyy-MM-dd hh:mm:ss"
    public static String currentYMDHSTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long l = System.currentTimeMillis();
        return simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(l))));
    }

    //两天差值
    public static int gapDays(String startTrainTime, String callOutTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        try {
            Date parse = sdf.parse(startTrainTime);
            Date parse1 = sdf.parse(callOutTime);
            if (parse1.getTime() >= parse.getTime())
                days = (int) ((parse1.getTime() - parse.getTime()) / 1000 / 3600 / 24);//算出具体的天数
        } catch (ParseException e) {
            log.error(String.valueOf(e));
        }
        return days;
    }

    //根据时间获取年周字符串
    public static String getYearWeekStr(Date date) {
        return new SimpleDateFormat("yyyy-ww").format(date);
    }

    //根据时间获取年月日字符串
    public static String getYearMonthDayStr(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    //根据时间获取年月字符串
    public static String getYearMonthStr(Date date) {
        return new SimpleDateFormat("yyyy-MM").format(date);
    }

    //把时间加上amount天
    public static Date addDays(Date date, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, amount);
        return calendar.getTime();
    }

    //把时间加上amount周
    public static Date addWeek(Date date, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.WEEK_OF_MONTH, amount);
        return calendar.getTime();
    }

    //把时间加上amount月
    public static Date addMonth(Date date, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, amount);
        return calendar.getTime();
    }

//根据传入的字符串获取开始时间
    public static Date getBeginDate(String str) {
        switch (str.length()) {
            //传入的字符串是7表示月
            case 7:
                return getDateByMonthString(str);
            //传入的字符串是10表示日
            case 10:
                return getDateByDateString(str);
            //默认是年
            default:
                return getDateByYearString(str);
        }
    }

    //根据传入的字符串获取结束时间
    public static Date getEndDate(String str) {
        switch (str.length()) {
            //传入的字符串是7表示月
            case 7:
                return addMonth(getDateByMonthString(str), 1);
            //传入的字符串是10表示日
            case 10:
                return addDays(getDateByDateString(str), 1);
            //默认是年
            default:
                return addYear(getDateByYearString(str), 1);
        }
    }

    //根据年度字符串获取时间
    public static Date getDateByYearString(String yearString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        try {
            return sdf.parse(yearString);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    //根据月份字符串获取时间
    public static Date getDateByMonthString(String monthString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            return sdf.parse(monthString);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    //根据日期字符串获取时间
    public static Date getDateByDateString(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(dateString);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    //String 转date "yyyy-MM-dd HH:mm:ss"
    public static Date StringToDateAll(String dateStr) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            date = sf.parse(dateStr);
            //打印Date
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    //根据时间获取凌晨的时间
    public static Date getDayBeginTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    //把时间加上amount分钟
    public static Date addMinute(Date date, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, amount);
        return calendar.getTime();
    }

    //把时间加上amount小时
    public static Date addHours(Date date, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, amount);
        return calendar.getTime();
    }

    //把时间加上amount天
    public static Date addDays(Date date, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, amount);
        return calendar.getTime();
    }

    //把时间加上amount月
    public static Date addMonth(Date date, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, amount);
        return calendar.getTime();
    }

    //把时间加上amount年
    public static Date addYear(Date date, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, amount);
        return calendar.getTime();
    }

    //后n天时间分秒时
    public static String AfterCurrentYMDHMS(Date date, int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, +n);
        Date date1 = calendar.getTime();
        return sdf.format(date1);
    }

    //根据时间获取年字符串
    public static String getYearStr(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        return formatter.format(date);
    }

    //根据时间获取年月字符串
    public static String getYearMonthStr(Date date) {
        return new SimpleDateFormat("yyyy-MM").format(date);
    }

    //根据时间获取年月日字符串
    public static String getYearMonthDayStr(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    //获取12/12月/12天之前的日期
    public static Date getBeforeTwelve(Date date, int type) {
        switch (type) {
            case 7:
                return addMonth(date, -12);
            case 10:
                return addDays(date, -12);
            default:
                return addYear(date, -12);
        }
    }

    //时间相减获取小时值
    public static Double getHourGap(Date beginTime, Date endTime) {
        if (beginTime == null) return null;
        if (endTime == null) endTime = new Date();
        return (endTime.getTime() - beginTime.getTime()) / 3600000D;
    }

    public static Date getAddDate(String year, String month, String day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(year+"-"+month+"-"+day);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    //获取当天时分秒 "yyyy-MM-dd hh:mm:ss"
    public static String currentYMDHSTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long l = System.currentTimeMillis();
        return simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(l))));
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值