zoukankan      html  css  js  c++  java
  • LocalDate和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);
        }
    java.sql.Date与java.util.Date的转化
    java.util.Date date2 = new java.util.Date(new java.sql.Date().getTime());
    java.sql.Date date1 = new java.sql.Date(new java.util.Date().getTime());

    参考https://blog.csdn.net/a25115/article/details/40380667

  • 相关阅读:
    NX 8.5 License Server Firewall Setting
    Cisco ASA intra-interface routing
    How to configure windows machine to allow file sharing with dns alias (CNAME)
    Install unifi controller on CentOS
    Android图片加载框架Glide用法
    判断app是否已启动
    SharedPreferences的一个工具类适合的数据类型包括String、Integer、Boolean、Float、Long
    android获取缓存大小和清除缓存
    对字符串进行MD5加密工具类
    android代码设置RelativeLayout的高度
  • 原文地址:https://www.cnblogs.com/grasp/p/12145801.html
Copyright © 2011-2022 走看看