LocalDateTime日期时间格式化
LocalDateTime localDateTime = LocalDateTime.now()
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
));
localDateTime.format(`DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
测试1
public static void main(String...arguments)throws Exception{
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));
System.out.println(now.format(DateTimeFormatter.ISO_DATE));
System.out.println(now.format(DateTimeFormatter.ISO_DATE_TIME));
System.out.println(now.format(DateTimeFormatter.ISO_TIME));
// System.out.println(now.format(DateTimeFormatter.ISO_INSTANT)); //会报错
System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_TIME));
// System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_DATE)); //会报错
// System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); //会报错
// System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_TIME)); //会报错
System.out.println(now.format(DateTimeFormatter.ISO_ORDINAL_DATE));
System.out.println(now.format(DateTimeFormatter.ISO_WEEK_DATE));
// System.out.println(now.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); //会报错
// System.out.println(now.format(DateTimeFormatter.RFC_1123_DATE_TIME)); //会报错
}
测试2文章来源:https://www.toymoban.com/news/detail-480612.html
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TestLocalDateTime230612 {
public static void main(String...arguments)throws Exception{
LocalDateTime now = LocalDateTime.now();
pln("原样toString()" , now.toString());
pln("DateTimeFormatter.ofPattern(\"yyyy-MM-dd HH:mm:ss\")" , now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
pln("DateTimeFormatter.BASIC_ISO_DATE)" , now.format(DateTimeFormatter.BASIC_ISO_DATE));
pln("DateTimeFormatter.ISO_DATE" , now.format(DateTimeFormatter.ISO_DATE));
pln("DateTimeFormatter.ISO_DATE_TIME)" , now.format(DateTimeFormatter.ISO_DATE_TIME));
pln("DateTimeFormatter.ISO_TIME)" , now.format(DateTimeFormatter.ISO_TIME));
// pln("" , now.format(DateTimeFormatter.ISO_INSTANT)); //会报错
pln("DateTimeFormatter.ISO_LOCAL_DATE)" , now.format(DateTimeFormatter.ISO_LOCAL_DATE));
pln("DateTimeFormatter.ISO_LOCAL_DATE_TIME)" , now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
pln("DateTimeFormatter.ISO_LOCAL_TIME)" , now.format(DateTimeFormatter.ISO_LOCAL_TIME));
// pln("" , now.format(DateTimeFormatter.ISO_OFFSET_DATE)); //会报错
// pln("" , now.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); //会报错
// pln("" , now.format(DateTimeFormatter.ISO_OFFSET_TIME)); //会报错
pln("DateTimeFormatter.ISO_ORDINAL_DATE)" , now.format(DateTimeFormatter.ISO_ORDINAL_DATE));
pln("DateTimeFormatter.ISO_WEEK_DATE)" , now.format(DateTimeFormatter.ISO_WEEK_DATE));
// pln("" , now.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); //会报错
// pln("" , now.format(DateTimeFormatter.RFC_1123_DATE_TIME)); //会报错
}
static void pln(String a, Object b) {
while(a.length()<"DateTimeFormatter.ofPattern(\"yyyy-MM-dd HH:mm:ss\")".length())a+=" ";
System.out.println (a + " 的效果: " + b);
}
}
测试2的结果文章来源地址https://www.toymoban.com/news/detail-480612.html
原样toString() 的效果: 2023-06-12T22:50:54.825257400
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") 的效果: 2023-06-12 22:50:54
DateTimeFormatter.BASIC_ISO_DATE) 的效果: 20230612
DateTimeFormatter.ISO_DATE 的效果: 2023-06-12
DateTimeFormatter.ISO_DATE_TIME) 的效果: 2023-06-12T22:50:54.8252574
DateTimeFormatter.ISO_TIME) 的效果: 22:50:54.8252574
DateTimeFormatter.ISO_LOCAL_DATE) 的效果: 2023-06-12
DateTimeFormatter.ISO_LOCAL_DATE_TIME) 的效果: 2023-06-12T22:50:54.8252574
DateTimeFormatter.ISO_LOCAL_TIME) 的效果: 22:50:54.8252574
DateTimeFormatter.ISO_ORDINAL_DATE) 的效果: 2023-163
DateTimeFormatter.ISO_WEEK_DATE) 的效果: 2023-W24-1
到了这里,关于Java8日期时间类LocalDateTime格式化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!