使用 LocalDateTime 查询今天日期字符串
以下是使用 Java 8 中的 LocalDateTime 类查询今天日期字符串的示例代码:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String today = now.format(formatter);
System.out.println(today);
}
}
输出:
2021-09-28
解释:
LocalDateTime.now()获取当前日期时间对象。DateTimeFormatter.ofPattern("yyyy-MM-dd")创建日期格式化器,指定日期格式为 "yyyy-MM-dd"。now.format(formatter)将当前日期时间对象按照指定格式进行格式化,返回格式化后的日期字符串。System.out.println(today)输出日期字符串。
原文地址: https://www.cveoy.top/t/topic/bsQL 著作权归作者所有。请勿转载和采集!