LocalDateTime java8
public static void main(String[] args) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startTime = LocalDateTime.now();
LocalDateTime endTime = startTime.plusDays(7);
System.out.println("开始时间:" + startTime.format(df) + ",结束时间:" + endTime.format(df));
LocalDateTime dateTest = LocalDateTime.parse("2018-02-30 12:12:12", df);
System.out.println("时间自动转化:" + dateTest.toString());
int daysNum = Period.between(startTime.toLocalDate(), endTime.toLocalDate()).getDays();
int monthNum = Period.between(startTime.toLocalDate(), endTime.toLocalDate()).getMonths();
System.out.println("相差天数:" + daysNum);
System.out.println("相差月数:" + monthNum);
System.out.println("当前时间向前推6天:" + LocalDateTime.now().minusDays(6));
System.out.println("当前时间向前推6小时:" + LocalDateTime.now().minusHours(6));
}
public static void main(String[] args) {
//一周从周日开始
WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 7);
LocalDate today = LocalDate.now();
System.out.println(LocalDateUtil.getDateAsString(today) + "今天是今年第" + today.get(weekFields.weekOfYear()) + "周");
}
注意:在后期使用Period.between()方法来获取,相差天数、相差月数的时候,发现只能计算同月的天数、同年的月数,不能计算隔月的天数以及隔年的月数!!!相当的坑
System.out.println(LocalDate.now().toEpochDay() - LocalDate.now().minusDays(5).toEpochDay());
System.out.println(LocalDate.now().until(LocalDate.now().minusDays(5),ChronoUnit.MONTHS));