1.获取年月日
示例代码:
// current date
LocalDate localDate = LocalDate.now();
System.out.println("Current date is:");
System.out.println(localDate.getYear());
System.out.println(localDate.getMonth());
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.get(ChronoField.MONTH_OF_YEAR));
// Date format
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(localDate.format(dateTimeFormatter));
// specific date
System.out.println("Specific date is:");
LocalDate specificDate = LocalDate.of(2018,10,23);
System.out.println(specificDate.format(dateTimeFormatter));
// TemporalAdjusters static method
LocalDate anothorLocalDate = localDate.with(firstDayOfYear());
System.out.println(anothorLocalDate.format(dateTimeFormatter));
打印结果如下:
Current date is:
2019
OCTOBER
23
10
2019-10-23
Specific date is:
2018-10-23
2019-01-01
2.获取时分秒
// current time
LocalTime localTime = LocalTime.now();
System.out.println("Current time is:");
System.out.println(localTime.getHour());
System.out.println(localTime.getMinute());
System.out.println(localTime.get(ChronoField.MINUTE_OF_HOUR));
// Time format
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss");
System.out.println(localTime.format(dateTimeFormatter));
// specific time
System.out.println("Specific time is:");
LocalTime specificTime = LocalTime.of(9,35,23);
System.out.println(specificTime.format(dateTimeFormatter));
3.获取年月日时分秒
// current time
LocalDateTime localDateTime = LocalDateTime.now();
// Time format
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
System.out.println(localDateTime.format(dateTimeFormatter));
4.日期加减
LocalDate localDate = LocalDate.now();
localDate = localDate.minusDays(7);//减7天
// 日期差(按月计算)
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
LocalDate startDate = LocalDate.parse(parameters.get("yearMonthS"), dateTimeFormatter);
LocalDate endDate = LocalDate.parse(parameters.get("yearMonthE"), dateTimeFormatter);
ChronoUnit.MONTHS.between(startDate, endDate)
5.日期格式化
## String转LocalDateTime
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05",df);
## String转LocalDate
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate ld = LocalDate.parse("2017-09-28",df);
## LocalDate转String
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now();
System.out.println(localDate.format(dateTimeFormatter));
## LocalDateTime转String
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
System.out.println(localDateTime.format(dateTimeFormatter));
## LocalDateTime转LocalDate
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
## LocalDate转LocalDateTime(3种方式)
LocalDate localDate = LocalDate.parse("2019-01-04");
LocalDateTime localDateTime1 = localDate.atStartOfDay();
LocalDateTime localDateTime2 = localDate.atTime(LocalTime.now());
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = localDate.atTime(04, 30, 56);
System.out.println(localDateTime3);
References
最新评论
网飞没问题, 迪士尼+有解决方案么?
pp助手是安卓手机用的,根本下载用不来苹果
已解决
这样的话数据库里的结构为{"attachment":{"content":"xxx"}}, 要怎么才能变成{"content":"xxx"},从而使结构保持一致?
赞! make test不过的坑都写到的,谢谢楼主~
谢谢你
用了root用户还是一直502是怎么回事呢
student id 是空的