java get the date one day before the specified date

Get the time of the specified date, the previous month or the previous year or the day before the current date

Java gets the time of the last month of the specified date, and the time of the previous year of the specified date: don't say much, just go to the code, and the comments are very complete

public static void main(String[] args) throws Exception{
        //首先指定我们的日期格式,主要用于时间解析与格式化
        //可以是 yyyyMM 、 yyyy-MM 、 yyyy/MM 主要看各位要用到什么格式的时间
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        //指定的日期,可以当参数传进来,这里我直接写死,也可以是当前时间
        String date1 = "202101";
        //字符串类型的时间转换
        Date parse = format.parse(date1);
        //获取指定的时间点
        Calendar calendar = Calendar.getInstance();
        // 将指定时间设置为当前时间
        calendar.setTime(parse); 
        // 设置为上一个年   Calendar.YEAR   设置为上一月 Calendar.MONTH  设置为上一天 Calendar.DATE
        // --> 其他的可以自己看情况使用
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
        //获取计算结束的时间
        parse = calendar.getTime();
        //转换时间格式
        String accDate = format.format(parse);
        //返回 
        System.out.println("上年同期 --->"+accDate);
    }

The results of the operation are as follows:
Insert picture description here

Insert picture description here

ok! Simply record and get the date of the previous day of the previous year of the specified date!

Guess you like

Origin blog.csdn.net/xaiobaicai/article/details/112978044