• java8 日期Date、LocalDate、LocalDateTime、字符串String的转换


    LocalDate转Date

        /**
         *
         * LocalDate转Date
         * @param localDate
         * @return
         */
        public static Date toDate(LocalDate localDate) {
            return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        }
    

    LocalDateTime转 Date

       /**
         * LocalDateTime转 Date
         * @param localDateTime
         * @return
         */
        public static Date toDate(LocalDateTime localDateTime) {
             return Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
        }
    

    LocalDateTime转String

        /**
         * LocalDateTime转String
         * @param localDateTime
         * @param pattern 格式,类似 yyyy-MM-dd HH:mm:ss
         * @return
         */
        public static String formatToString(LocalDateTime localDateTime, String pattern) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return localDateTime.format(formatter);
        }
    
    
    

    String转LocalDateTime

        /**
         * String转 LocalDateTime
         *
         * @param dateTimeStr 日期的字符串
         * @param pattern 格式,类似 yyyy-MM-dd HH:mm:ss
         * @return
         */
        public  static LocalDateTime toLocalDateTime(String dateTimeStr, String pattern) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(dateTimeStr, formatter);
        }
    
    

    String转localDateTime

        /**
         * String转localDateTime
         *
         * @param dateTimeStr 日期的字符串
         * @param pattern 格式,类似 yyyy-MM-dd HH:mm:ss
         * @return
         */
        public  static LocalDateTime toLocalDateTime(String dateTimeStr, String pattern) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(dateTimeStr, formatter);
        }
    
    

    String转LocalDateTime

        /**
         * String转LocalDateTime
         *
         * @param dateTimeStr 日期的字符串
         * @param pattern 格式,类似 yyyy-MM-dd HH:mm:ss
         * @return
         */
        public  static LocalDateTime toLocalDateTime(String dateTimeStr, String pattern) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(dateTimeStr, formatter);
        }
    
    

    Date转LocalDateTime

        /**
         * Date转LocalDateTime
         * @param date 日期
         * @return
         */
        public static LocalDateTime toLocalDateTime(Date date) {
            return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        }
    

    Date转String

        /**
         * Date转String
         * @param date  日期
         * @param pattern 格式,类似 yyyy-MM-dd HH:mm:ss
         * @return
         */
        public static String formatToString(Date date, String pattern) {
            if (date == null) {
                return "";
            }
            LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return localDateTime.format(formatter);
        }
    

    String转Date

     /**
         * String转Date
         * @param dateTimeStr 字符串
         * @param pattern 格式,类似 yyyy-MM-dd HH:mm:ss
         * @return
         */
        public static Date toDate(String dateTimeStr, String pattern) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, formatter);
            return Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
        }
    
    

    参考资料:

    LocalDateTime的理解: https://www.cnblogs.com/expiator/p/12815547.html

  • 相关阅读:
    [转]tf.summary() 用法
    PASCAL VOC工具包解读
    [ERROR] 安装完Detectron后出现 cython_nms.so: undefined symbol: PyFPE_jbuf
    用Tensorflow做蝴蝶检测
    双系统,重装ubuntu后无法进入windows
    [Error]NodeDef mentions attr 'identical_element_shapes' not in Op<name=TensorArrayV3;
    [转]调试 smallcorgi/Faster-RCNN_TF 的demo过程遇到的问题
    js交互轮播图
    js取俩个数之间的随机数
    原生js实现触摸滚动轮播图
  • 原文地址:https://www.cnblogs.com/expiator/p/16222440.html
Copyright © 2020-2023  润新知