使用 LocalDateTime 获取当前日期字符串
以下是使用 LocalDateTime 获取当前日期字符串的示例代码:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentDate {
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("Current date and time: " + formattedDate);
}
}
该代码将获取当前日期和时间,并使用指定的格式将其格式化为字符串。在本例中,使用格式字符串“yyyy-MM-dd HH:mm:ss”,即年-月-日 时:分:秒的格式。您可以根据需要更改格式字符串。
原文地址: https://www.cveoy.top/t/topic/bMQ5 著作权归作者所有。请勿转载和采集!