import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample {
public static void main(String[] args) {
// 获取当前日期和时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("当前日期和时间: " + currentDateTime);
// 创建特定日期和时间的对象
LocalDateTime specificDateTime = LocalDateTime.of(2023, 10, 5, 14, 30, 45);
System.out.println("特定日期和时间: " + specificDateTime);
// 格式化日期和时间
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = currentDateTime.format(formatter);
System.out.println("格式化后的日期和时间: " + formattedDateTime);
// 解析字符串为LocalDateTime对象
String dateTimeString = "2023-10-05 14:30:45";
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println("解析后的日期和时间: " + parsedDateTime);
}
}
LocalDateTime.now() 方法可以获取当前的日期和时间。LocalDateTime.of(year, month, day, hour, minute, second) 方法可以创建一个指定日期和时间的 LocalDateTime 对象。DateTimeFormatter 可以将 LocalDateTime 对象格式化为指定的字符串格式。LocalDateTime 对象:使用 LocalDateTime.parse() 方法可以将格式化的字符串解析回 LocalDateTime 对象。上一篇:java base64解码
下一篇:java 异步
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站