import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class GetCurrentMonth {
public static void main(String[] args) {
// 获取当前时间
LocalDateTime now = LocalDateTime.now();
// 格式化为月份
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
String currentMonth = now.format(formatter);
// 输出当前月份
System.out.println("当前月份: " + currentMonth);
}
}
java.time.LocalDateTime
和 java.time.format.DateTimeFormatter
来处理时间和日期格式。LocalDateTime.now()
获取当前的日期和时间。DateTimeFormatter.ofPattern("yyyy-MM")
将当前时间格式化为 "年-月" 的形式。上一篇:java缓存
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站