import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateToYyyymm {
    public static void main(String[] args) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        System.out.println("当前日期: " + currentDate);
        // 定义格式化模式为 yyyymm
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        // 将日期格式化为 yyyymm 格式
        String formattedDate = currentDate.format(formatter);
        System.out.println("转换后的日期: " + formattedDate);
    }
}java.time.LocalDate 和 java.time.format.DateTimeFormatter 来处理日期和日期格式化。LocalDate.now() 获取当前的日期。DateTimeFormatter.ofPattern("yyyyMM") 创建一个格式化器,指定输出格式为 yyyymm。currentDate.format(formatter) 将日期对象转换为指定格式的字符串。如果你需要将其他日期转换为 yyyymm 格式,只需将 LocalDate.now() 替换为你想要转换的具体日期即可。
上一篇:java reflections
下一篇:java.lang.object
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站