java代码:
//当前日期
LocalDate now = LocalDate.now();
String format = now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
System.out.println("LocalDate-FULL: "+format);
String format3 = now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
System.out.println("LocalDate-LONG: "+format3);
String format4 = now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
System.out.println("LocalDate-SHORT: "+format4);
//一天后的日期
LocalDate localDate = now.plusDays(1);
String format1 = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
System.out.println("plusDays-FULL: "+format1);
//前一天的日期
String format2 = now.minusDays(1).format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println("minusDays-MEDIUM: "+format2);
//自定义格式化
String ofPattern = now.minusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
System.out.println("minusDays-ofPattern:"+ofPattern);
打印结果:
LocalDate-FULL: 2017年9月23日 星期六
LocalDate-LONG: 2017年9月23日
LocalDate-SHORT: 17-9-23
plusDays-FULL: 2017年9月24日 星期日
minusDays-MEDIUM: 2017-9-22
minusDays-ofPattern:20170922
这篇博客介绍了Java中LocalDate的使用,包括获取当前日期、加减天数以及日期格式化的不同风格。示例代码展示了如何利用DateTimeFormatter进行FULL、LONG、SHORT格式的日期输出,以及自定义格式化为'yyyyMMdd'。

被折叠的 条评论
为什么被折叠?



