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

更新于: 30-Jul-2019

162 次浏览

开启 职业生涯

通过完成课程获取认证

开始
广告
© . All rights reserved.