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 好听的元旦简短祝福语