import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateFormatExample {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 定义时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 将当前日期时间格式化为字符串
String formattedDate = now.format(formatter);
// 输出格式化后的时间
System.out.println("当前时间: " + formattedDate);
// 将字符串解析为 LocalDateTime 对象
LocalDateTime parsedDate = LocalDateTime.parse(formattedDate, formatter);
// 输出解析后的时间
System.out.println("解析后的时间: " + parsedDate);
}
}
LocalDateTime
用于表示日期和时间,DateTimeFormatter
用于定义日期时间的格式。LocalDateTime.now()
获取当前的日期和时间。DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
定义一个时间格式模板。LocalDateTime
对象格式化为指定格式的字符串。LocalDateTime
对象。LocalDateTime
对象。上一篇:java map的常用方法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站