LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59));
Date now = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
这段代码的作用是将当前日期的最后一秒转换为java.util.Date对象。
首先,使用LocalDateTime.of()方法创建一个包含当前日期和时间的LocalDateTime对象,时间设置为23:59:59。然后,使用atZone()方法将LocalDateTime对象转换为带有系统默认时区的ZonedDateTime对象。接下来,使用toInstant()方法将ZonedDateTime对象转换为Instant对象。最后,使用Date.from()方法将Instant对象转换为java.util.Date对象。这样,就得到了表示当前日期最后一秒的Date对象。
例如,如果当前日期是2022年1月1日,那么执行这段代码后,localDateTime对象将表示2022年1月1日的23:59:59。通过转换,最终得到的Date对象将表示同样的日期和时间。这对于需要使用旧的java.util.Date类的代码非常有用,可以将新的java.time.LocalDateTime对象转换为旧的Date对象进行处理。
获取当前日期,并在当前日期的基础上加上15天,得到一个新的日期:
SimpleDateFormat format = new SimpleDateFormat(BaseConstants.Pattern.DATETIME);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 15);