import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 定义格式化模式为年月,例如:2023-10
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
// 格式化当前日期时间为指定的年月格式
String currentYearMonth = now.format(formatter);
// 输出结果
System.out.println("当前年月: " + currentYearMonth);
}
}
LocalDateTime
和 DateTimeFormatter
是 Java 8 引入的时间处理类,用于获取当前日期时间和格式化日期。LocalDateTime.now()
获取当前的日期和时间。DateTimeFormatter.ofPattern("yyyy-MM")
定义一个格式化模式,将日期格式化为“年-月”的形式。now.format(formatter)
将当前日期时间格式化为指定的年月格式。System.out.println
输出格式化后的年月。上一篇:java 遍历数组
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站