JDK8 LocalDate、LocalDateTime的用法 LocalDate now = LocalDate.now(); //当前时间 int day = now.getDayOfYear(); //当前时间在所在年份的第几天 DayOfWeek week = now.getDayOfWeek(); //当前时间是星期几(英文显示) int getDayOfMonth = now.getDayOfMonth(); //当前日期是当月的第几天 Month getMonth = now.getMonth(); //当前月份(月份显示英文) int getMonthValue = now.getMonthValue(); //当前月份int类型 int lengthOfMonth = now.lengthOfMonth(); //当月有多少天 int lengthOfYear = now.lengthOfYear(); //当前时间所在年份有多少天 boolean isLeapYear = now.isLeapYear(); //当前时间所在年份是否闰月 LocalDate minusDays = now.minusDays(1); //当前时间减一天 LocalDate minusWeeks = now.minusWeeks(1); //当前时间减一周 LocalDate minusMonths = now.minusMonths(1); //当前时间减一个月 LocalDate minusYears = now.minusYears(1); //当前时间减一年 LocalDate plusDays = now.plusDays(1); //当前时间加一天 LocalDate plusWeeks = now.plusWeeks(1); //当前时间加一周 LocalDate plusMonths = now.plusMonths(1); //当前时间加一个月 LocalDate plusYears = now.plusYears(1); //当前时间加一年 LocalDate withDayOfMonth = now.withDayOfMonth(5); //设置当前月份的号数 如2020-12-05 这个5号就是自己设置的 LocalDate withDayOfYear = now.withDayOfYear(10); //设置当前年份中的号数 如2020-01-10 这个10号就是自己设置的 LocalDate withMonth = now.withMonth(8); //设置月份 LocalDate withYear = now.withYear(2019); //设置年份 LocalDate tomorrow = LocalDate.now().plusDays(1); boolean isAfter = now.isAfter(tomorrow); //比较当前时间是否在tomorrow之后 是为true、否为false boolean isBefore = now.isBefore(tomorrow); //比较当前时间是否在tomorrow之前 是为true、否为false LocalDateTime nowTime = LocalDateTime.now(); //获取当前时间带时分秒 int hour = nowTime.getHour(); //获取当前时间的小时 int minute = nowTime.getMinute(); //获取当前时间的分钟 int second = nowTime.getSecond(); //获取当前时间的秒数 int nano = nowTime.getNano(); //获取当前时间的纳秒值 LocalDateTime withHour = nowTime.withHour(20); //设置当前时间的小时 返回值:2020-12-09T20:12:17.173 LocalDateTime withMinute = nowTime.withMinute(55); //设置当前时间的分钟数 返回值:2020-12-09T17:55:47.918 LocalDateTime withSecond = nowTime.withSecond(25); //设置当前时间的秒数 返回值:2020-12-09T17:13:25.278 LocalDate toLocalDate = nowTime.toLocalDate(); //获取当前时间只有年月日 LocalTime toLocalTime = nowTime.toLocalTime(); //获取当前时间的时分秒毫秒
记录一下 以免自己忘记了。