Java 获取上个月的年月字符串 - 代码示例
在Java中,可以使用java.time.LocalDate来获取当前系统日期,并使用java.time.format.DateTimeFormatter来格式化日期。\n\n以下是获取当前系统月份的上个年月字符串格式的代码示例:\n\njava\nimport java.time.LocalDate;\nimport java.time.format.DateTimeFormatter;\n\npublic class Main {\n public static void main(String[] args) {\n // 获取当前系统日期\n LocalDate currentDate = LocalDate.now();\n \n // 获取上个月的日期\n LocalDate previousMonth = currentDate.minusMonths(1);\n \n // 定义日期格式化器\n DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");\n \n // 格式化日期为字符串\n String previousMonthString = previousMonth.format(formatter);\n \n // 打印结果\n System.out.println(previousMonthString);\n }\n}\n\n\n运行以上代码,将输出当前系统月份的上个年月字符串格式,例如:2021-06。
原文地址: https://www.cveoy.top/t/topic/pG44 著作权归作者所有。请勿转载和采集!