import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
System.out.println("当前日期: " + currentDate);
// 获取当前日期和时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("当前日期和时间: " + currentDateTime);
// 格式化日期
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = currentDateTime.format(formatter);
System.out.println("格式化后的日期和时间: " + formattedDate);
}
}
LocalDate.now() 方法可以获取当前的日期(年、月、日)。LocalDateTime.now() 方法可以获取当前的日期和时间(包括小时、分钟、秒等)。DateTimeFormatter 类可以将日期和时间格式化为指定的字符串格式。这里我们使用了 "yyyy-MM-dd HH:mm:ss" 的格式。如果你需要其他特定的日期操作或格式,请告诉我!
上一篇:java的输入语句怎么写
下一篇:java程序的入口方法是
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站