Java Data Type How to - Convert LocalDate to java.util.Date by timezone offset








Question

We would like to know how to convert LocalDate to java.util.Date by timezone offset.

Answer

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Date;
//from   w w w .  j  av  a  2s .com
public class Main {

  public static void main(String[] args) {
    System.out.println(fromLocalDateIn(LocalDate.now(), ZoneOffset.UTC));
  }

  public static Date fromLocalDateIn(LocalDate date, ZoneOffset timeZone) {
    return Date.from(date.atStartOfDay().toInstant(timeZone));
  }
}

The code above generates the following result.