Java 程序通过时区偏移将 LocalDate 转换为 java.util.Date
将 LocalDate 设置为现在 −
LocalDate date = LocalDate.now();
现在,设置时区偏移 −
ZoneOffset timeZone = ZoneOffset.UTC;
将 LocalDate 转换为 java.util.Date −
Date.from(date.atStartOfDay().toInstant(timeZone))
示例
import java.time.LocalDate; import java.time.ZoneOffset; import java.util.Date; public class Demo { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println("Date = "+date); ZoneOffset timeZone = ZoneOffset.UTC; System.out.println(Date.from(date.atStartOfDay().toInstant(timeZone))); } }
输出
Date = 2019-04-19 Fri Apr 19 05:30:00 IST 2019
广告