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 生日广州男生祝福语简短