public class Subject {
private static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
//当前原生格式时间
System.out.println(“当前原生格式时间:” + currentDateTime);
//当前指定格式时间
String format = currentDateTime.format(FORMATTER);
System.out.println(“当前指定格式时间:” + format);
//当前时间向后推一天
LocalDateTime nextDateTime = currentDateTime.plusDays(1);
System.out.println(“当前时间向后推一天” + nextDateTime);
//当前时间向前推一天
LocalDateTime afterDateTime = currentDateTime.minusDays(1);
System.out.println(“当前时间向前推一天” + afterDateTime);
//当前时间推一天一小时一分钟
LocalDateTime addDateTime = currentDateTime.plusDays(1).plusHours(1).plusMinutes(1);
System.out.println(“当前时间推一天一小时一分钟” + addDateTime);
//判断目标天是否在当前天后
boolean result = currentDateTime.isBefore(addDateTime);
System.out.println(“判断目标天是否在当前天后” + result);
//获取当前月的第一天以及最后一天
LocalDateTime firstday = currentDateTime.with(TemporalAdjusters.firstDayOfMonth());
LocalDateTime lastDay = currentDateTime.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(“当月第一天:” + firstday);
System.out.println(“当月最后一天:” + lastDay);
//两个时间相差几天
long days = lastDay.toLocalDate().toEpochDay() - firstday.toLocalDate().toEpochDay();
Period between = Period.between(firstday.toLocalDate(), lastDay.toLocalDate());
System.out.println(“两个时间相差:” + days);
System.out.println(“两个时间相差:” + between.getDays());
//计算当月有几天
System.out.println(“当月有几天”+lastDay.toLocalDate().getDayOfYear());
}
}
当前原生格式时间:2020-01-15T10:54:16.519
当前指定格式时间:2020-01-15 10:54:16
当前时间向后推一天2020-01-16T10:54:16.519
当前时间向前推一天2020-01-14T10:54:16.519
当前时间推一天一小时一分钟2020-01-16T11:55:16.519
判断目标天是否在当前天后true
当月第一天:2020-01-01T10:54:16.519
当月最后一天:2020-01-31T10:54:16.519
两个时间相差:30
两个时间相差:30
当月有几天31
LocalDateTime 随行笔记
最新推荐文章于 2024-04-23 18:52:22 发布