根据传入时间和往前数量获取几秒前,几天前等

103 阅读2分钟
@Data
public class DateUtil {

    public static void main(String[] args) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("一秒钟前:" + format.format(getMinute(transferString2Date("2022-12-12 12:12:12"),3)));
    }

    /**
     * @description 字符串类型转换为Date类型
     * @author XianZeng.Zhang
     * @date 2023-01-09 13:32
     * @param
     * @return null
    */
    public static Date transferString2Date(String s) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
        } catch (ParseException e) {
            //LOGGER.error("时间转换错误, string = {}", s, e);
        }
        return date;
    }
   /**
    * @description Date类型转String
    * @author XianZeng.Zhang
    * @date 2023-01-09 13:41
    * @param date
    * @return null
   */
    public static String transferDateAndString(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        return time;
    }

    /**
     * @description 几秒钟前
     * @author XianZeng.Zhang
     * @date 2023-01-09 13:12
     * @param time 某个时间
     * @param timeScope 设置前几秒
     * @return null
    */
    public static Date getSecond(Date time,Integer timeScope) {
        //获取当前时间
        Calendar cal = Calendar.getInstance();
        //赋值为传入时间
        cal.setTime(time);
        cal.add(Calendar.SECOND, -timeScope);
        return cal.getTime();
    }

    /**
     * @description 几分钟前
     * @author XianZeng.Zhang
     * @date 2023-01-09 13:12
     * @param time 某个时间
     * @param timeScope 设置前几分钟
     * @return null
     */
    public static Date getMinute(Date time,Integer timeScope) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(Calendar.MINUTE, -timeScope);
        return cal.getTime();
    }

    /**
     * @description 几小时前
     * @author XianZeng.Zhang
     * @date 2023-01-09 13:12
     * @param time 某个时间
     * @param timeScope 设置前几小时
     * @return null
     */
    public static Date getHourOfDay(Date time,Integer timeScope) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(Calendar.HOUR_OF_DAY, -timeScope);
        return cal.getTime();
    }

    /**
     * @description 几天前
     * @author XianZeng.Zhang
     * @date 2023-01-09 13:12
     * @param time 某个时间
     * @param timeScope 设置前几天
     * @return null
     */
    public static Date getDate(Date time,Integer timeScope) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(Calendar.DATE, -timeScope);
        return cal.getTime();
    }

    /**
     * @description 几周前
     * @author XianZeng.Zhang
     * @date 2023-01-09 13:12
     * @param time 某个时间
     * @param timeScope 设置前几周
     * @return null
     */
    public static Date getWeekOfYear(Date time,Integer timeScope) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(Calendar.WEEK_OF_YEAR, -timeScope);
        return cal.getTime();
    }

    /**
     * @description 几月前
     * @author XianZeng.Zhang
     * @date 2023-01-09 13:12
     * @param time 某个时间
     * @param timeScope 设置前几月
     * @return null
     */
    public static Date getMonth(Date time,Integer timeScope) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(Calendar.MONTH, -timeScope);
        return cal.getTime();
    }

    /**
     * @description 几年前
     * @author XianZeng.Zhang
     * @date 2023-01-09 13:12
     * @param time 某个时间
     * @param timeScope 设置前几年
     * @return null
     */
    public static Date getyear(Date time,Integer timeScope) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(Calendar.YEAR, -timeScope);
        return cal.getTime();
    }

}