Java Date get midnight of the following day via LocalTime and LocalDate

Description

Java Date get midnight of the following day via LocalTime and LocalDate

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    Date d = getMidnight();/* ww  w.  j a  v a 2  s  .  co  m*/
    System.out.println(d);
  }

  static public Date getMidnight() {
    final LocalTime midnight = LocalTime.MIDNIGHT;
    final LocalDate today = LocalDate.now(ZoneId.systemDefault());
    final LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
    final LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);
    return Date.from(tomorrowMidnight.atZone(ZoneId.systemDefault()).toInstant());
  }
}



PreviousNext

Related