在Java中,可以使用java.time.LocalDate来获取当前系统日期,并使用java.time.format.DateTimeFormatter来格式化日期。

以下是获取当前系统月份的上个年月字符串格式的代码示例:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // 获取当前系统日期
        LocalDate currentDate = LocalDate.now();
        
        // 获取上个月的日期
        LocalDate previousMonth = currentDate.minusMonths(1);
        
        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        
        // 格式化日期为字符串
        String previousMonthString = previousMonth.format(formatter);
        
        // 打印结果
        System.out.println(previousMonthString);
    }
}

运行以上代码,将输出当前系统月份的上个年月字符串格式,例如:2021-06

java 获取当前系统月份的上个年月字符串格式

原文地址: https://www.cveoy.top/t/topic/hXYk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录