java 时间篇(获取前一个月,前一天,前一个小时,前一分钟)

一:获取前一个月

    /**
     * @Description: 获取上个月
     */
    public static String getLastMonth() {
        LocalDate today = LocalDate.now();
        today = today.minusMonths(1);
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");
        return formatters.format(today);
    }

二:前一天

    /**
     * @Description: 获取前一天
     */
    public static String getDATE() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        Date start = c.getTime();
        return format.format(start);
    }

三:前一个小时

    /**
     * @Description: 获取上1个小时
     */
    public static String beforeOneHourToNowDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(calendar.getTime());
    }

四:前一分钟

    /**
     * @Description: 获取上1分钟
     */
    public static String beforeOneHourMINUTE() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, (calendar.get(Calendar.MINUTE) - 1));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(calendar.getTime());
    }

 

    原文作者:爱码仕1
    原文地址: https://blog.csdn.net/qq_41712834/article/details/103242477
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞