Java程序将java.util.Date转换为特定时区中的任何本地日期
首先,设置Date和ZoneId-
Date date = new Date(); ZoneId zone = ZoneId.systemDefault();
现在将java.util.date转换为localdate-
date.toInstant().atZone(zone).toLocalDate() date.toInstant().atZone(zone).toLocalTime() date.toInstant().atZone(zone).getHour() date.toInstant().atZone(zone).getMinute() date.toInstant().atZone(zone).getSecond()
示例
import java.time.ZoneId;
import java.util.Date;
public class Demo {
public static void main(String[] args) {
Date date = new Date();
ZoneId zone = ZoneId.systemDefault();
System.out.println("LocalDate = "+date.toInstant().atZone(zone).toLocalDate());
System.out.println("LocalTime= "+date.toInstant().atZone(zone).toLocalTime());
System.out.println("Hour = "+date.toInstant().atZone(zone).getHour());
System.out.println("Minute = "+date.toInstant().atZone(zone).getMinute());
System.out.println("Seconds = "+date.toInstant().atZone(zone).getSecond());
}
}输出结果
LocalDate = 2019-04-18 LocalTime= 23:25:09.708 Hour = 23 Minute = 25 Seconds = 9
热门推荐
10 给儿子生日祝福语 简短
11 高中升学宴祝福语简短
12 年底拜年祝福语大全简短
13 新年祝福语长辈简短红包
14 秋分祝福语简短文案
15 猪年幽默祝福语简短
16 年底老板祝福语简短的
17 生日祝福语简短独特格式
18 幼儿老师过年简短祝福语