-
Notifications
You must be signed in to change notification settings - Fork 46.1k
Closed
Labels
enhancementNew feature or request or suggestionNew feature or request or suggestionperfectImprove knowledge points or descriptionsImprove knowledge points or descriptions
Description
再来看一个使用 DateTimeFormatter 格式化日期的示例
LocalDateTime rightNow=LocalDateTime.now();
String date=DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(rightNow);
System.out.println(date);//2019-03-12T16:26:48.29
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
System.out.println(formatter.format(rightNow));//2019-03-12 16:26:48Java中,使用YYYY显示年份时,会显示当前时间所在周的年份,在跨年周会有问题。一般情况下都使用yyyy,来显示准确的年份。
e.g.:
LocalDateTime rightNow = LocalDateTime.of(2020, 12, 31, 12, 0, 0);
String date= DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(rightNow);
// 2020-12-31T12:00:00
System.out.println(date);
DateTimeFormatter formatterOfYYYY = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
// 2021-12-31 12:00:00
System.out.println(formatterOfYYYY.format(rightNow));
DateTimeFormatter formatterOfYyyy = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 2020-12-31 12:00:00
System.out.println(formatterOfYyyy.format(rightNow));Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request or suggestionNew feature or request or suggestionperfectImprove knowledge points or descriptionsImprove knowledge points or descriptions