Java Date add days via LocalDate

Description

Java Date add days via LocalDate

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

public class Main {
  public static void main(String[] argv) {
    System.out.println(new Date());
    /* www.  j  a  v  a  2s .  c  o  m*/
    System.out.println(plusDays(new Date(), 100));
  }

  public static Date plusDays(Date date, int days) {
    return localDateToDate(dateToLocalDate(date).plusDays(days));
  }

  public static Date localDateToDate(LocalDate localDate) {
    return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
  }

  public static LocalDate dateToLocalDate(Date date) {
    return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  }
}



PreviousNext

Related