// Java Time API 示例代码
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class JavaTimeExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
System.out.println("当前日期: " + currentDate);
// 获取当前时间
LocalTime currentTime = LocalTime.now();
System.out.println("当前时间: " + currentTime);
// 获取当前日期和时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("当前日期和时间: " + currentDateTime);
// 格式化日期时间
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = currentDateTime.format(formatter);
System.out.println("格式化后的日期时间: " + formattedDateTime);
// 解析字符串为日期时间
LocalDateTime parsedDateTime = LocalDateTime.parse("2023-10-05T14:30:00");
System.out.println("解析后的日期时间: " + parsedDateTime);
}
}
LocalDate 用于表示不带时间的日期。LocalTime 用于表示不带日期的时间。LocalDateTime 用于表示带日期和时间的对象。DateTimeFormatter 用于格式化和解析日期时间字符串。上一篇:java格式化日期
下一篇:java long 转 int
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站