zoukankan      html  css  js  c++  java
  • LocalDate/LocalDateTime/LocalTime与Date的互转

    // 01. java.util.Date --> java.time.LocalDateTime
    public void UDateToLocalDateTime() {
    java.util.Date date = new java.util.Date();
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    }
    
    // 02. java.util.Date --> java.time.LocalDate
    public void UDateToLocalDate() {
    java.util.Date date = new java.util.Date();
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    LocalDate localDate = localDateTime.toLocalDate();
    }
    
    // 03. java.util.Date --> java.time.LocalTime
    public void UDateToLocalTime() {
    java.util.Date date = new java.util.Date();
    Instant instant = date.toInstant();
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    LocalTime localTime = localDateTime.toLocalTime();
    }
    
    
    // 04. java.time.LocalDateTime --> java.util.Date
    public void LocalDateTimeToUdate() {
    LocalDateTime localDateTime = LocalDateTime.now();
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDateTime.atZone(zone).toInstant();
    java.util.Date date = Date.from(instant);
    }
    
    
    // 05. java.time.LocalDate --> java.util.Date
    public void LocalDateToUdate() {
    LocalDate localDate = LocalDate.now();
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
    java.util.Date date = Date.from(instant);
    }
    
    // 06. java.time.LocalTime --> java.util.Date
    public void LocalTimeToUdate() {
    LocalTime localTime = LocalTime.now();
    LocalDate localDate = LocalDate.now();
    LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
    ZoneId zone = ZoneId.systemDefault();
    Instant instant = localDateTime.atZone(zone).toInstant();
    java.util.Date date = Date.from(instant);
    }
  • 相关阅读:
    What are the difference between DDL, DML and DCL commands?
    Dingjun123 :使用Partitioned Outer Join实现稠化报表
    Oracle Clusters
    Google实验室能力倾向测试(第一题及解答)
    搜索系统中基于字典的逆向中文分词
    vc++ 深入浅出 窗口创建过程
    计算机网络基础知识1
    线性代数学习之对称矩阵与矩阵的SVD分解
    珍爱生命
    str2hash
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/11090146.html
Copyright © 2011-2022 走看看