zoukankan      html  css  js  c++  java
  • jdk8 时间转换工具类

    package com.idc.utils;
    
    
    import lombok.SneakyThrows;
    
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.LocalTime;
    import java.time.ZoneId;
    import java.time.format.DateTimeFormatter;
    import java.time.temporal.TemporalAdjusters;
    import java.util.Date;
    
    public class LocalDateUtil {
    
        /**
         * 日期格式yyyy-MM-dd
         */
        public static String DATE_PATTERN = "yyyy-MM-dd";
    
        /**
         * 日期格式yyyy年dd月
         */
        public static String DATE_PATTERN2 = "yyyy年MM月";
    
        /**
         * 日期格式yyyy年dd月
         */
        public static String DATE_PATTERN3 = "yyyy-MM";
    
        /**
         * 日期时间格式yyyy-MM-dd HH:mm:ss
         */
        public static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    
        /**
         * 时间格式yyyy-MM-dd HH:mm:ss
         */
        public static String TIME_PATTERN = "HH:mm:ss";
    
        /**
         * 获取默认时间格式: yyyy-MM-dd HH:mm:ss
         */
        public static DateTimeFormatter DEFAULT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss+08:00");
        public static DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        public static DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
        /**
         * 构造函数
         */
        private LocalDateUtil() {
            super();
        }
    
        /**
         * Date转LocalDateTime
         *
         * @param date Date对象
         * @return
         */
        public static LocalDateTime dateToLocalDateTime(Date date) {
            return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        }
    
        /**
         * LocalDateTime转换为Date
         *
         * @param dateTime LocalDateTime对象
         * @return
         */
        public static Date localDateTimeToDate(LocalDateTime dateTime) {
            return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
    
        /**
         * 格式化时间-默认yyyy-MM-dd HH:mm:ss格式
         *
         * @param dateTime LocalDateTime对象
         * @return
         */
        public static String formatLocalDateTime(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime, DATE_TIME_PATTERN);
        }
    
        /**
         * 格式化时间为字符串-默认yyyy-MM-dd 格式
         *
         * @param date LocalDate对象
         * @return
         */
        public static String formatLocalDateToStr(LocalDate date, String pattern) {
            if (date == null) {
                return "";
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return date.format(formatter);
        }
    
        /**
         * String 转时间-默认yyyy-MM-dd格式
         *
         * @param timeStr 字符串
         * @return
         */
        public static LocalDate parseStrToLocalDate(String timeStr) {
            if (timeStr == null || timeStr.isEmpty()) {
                return null;
            }
            return LocalDate.parse(timeStr, DEFAULT_DATE_FORMATTER);
        }
    
        /**
         * LocalDateTime
         *
         * @param localDateTime
         * @return yyyy-MM-dd
         */
        public static String parseLocalDateTimeToString(LocalDateTime localDateTime) {
            return localDateTime.format(DEFAULT_DATETIME_FORMATTER);
        }
    
        /**
         * 按pattern格式化时间-默认yyyy-MM-dd HH:mm:ss格式
         *
         * @param dateTime LocalDateTime对象
         * @param pattern  要格式化的字符串
         * @return
         */
        public static String formatLocalDateTime(LocalDateTime dateTime, String pattern) {
            if (dateTime == null) {
                return "";
            }
            if (pattern == null || pattern.isEmpty()) {
                pattern = DATE_TIME_PATTERN;
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return dateTime.format(formatter);
        }
    
        /**
         * String 转时间-默认yyyy-MM-dd HH:mm:ss格式
         *
         * @param timeStr 字符串
         * @return
         */
        public static LocalDateTime parseStrToLocalDateTime(String timeStr) {
            if (timeStr == null || timeStr.isEmpty()) {
                return null;
            }
            return LocalDateTime.parse(timeStr, DEFAULT_DATETIME_FORMATTER);
        }
    
        /**
         * 获取今天的00:00:00
         *
         * @return
         */
        public static String getDayStart() {
            return getDayStart(LocalDateTime.now());
        }
    
        /**
         * 获取今天的23:59:59
         *
         * @return
         */
        public static String getDayEnd() {
            return getDayEnd(LocalDateTime.now());
        }
    
        /**
         * 获取某天的00:00:00
         *
         * @param dateTime
         * @return
         */
        public static String getDayStart(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime.with(LocalTime.MIN));
        }
    
        /**
         * 获取某天的23:59:59
         *
         * @param dateTime
         * @return
         */
        public static String getDayEnd(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime.with(LocalTime.MAX));
        }
    
        /**
         * 获取本月第一天的00:00:00
         *
         * @return
         */
        public static String getFirstDayOfMonth() {
            return getFirstDayOfMonth(LocalDateTime.now());
        }
    
        /**
         * 获取本月最后一天的23:59:59
         *
         * @return
         */
        public static String getLastDayOfMonth() {
            return getLastDayOfMonth(LocalDateTime.now());
        }
    
        /**
         * 获取某月第一天的00:00:00
         *
         * @param dateTime LocalDateTime对象
         * @return
         */
        public static String getFirstDayOfMonth(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN));
        }
    
        /**
         * 获取某月最后一天的23:59:59
         *
         * @param dateTime LocalDateTime对象
         * @return
         */
        public static String getLastDayOfMonth(LocalDateTime dateTime) {
            return formatLocalDateTime(dateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX));
        }
    
        @SneakyThrows
        public static Date strToDate(String time) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter.parse(time);
        }
    
        /**
         * 测试
         *
         * @param args
         */
        public static void main(String[] args) {
            /*System.out.println(getDayStart());
            System.out.println(getDayEnd());
            System.out.println(getFirstDayOfMonth());
            System.out.println(getLastDayOfMonth());
            System.out.println(LocalDateUtil.formatDateTime(LocalDateTime.now(), LocalDateUtil.TIME_PATTERN));
            System.out.println(parseStrToLocalDateTime("2018-12-12 10:10:10"));*/
            System.out.println(formatLocalDateToStr(LocalDate.now(), DATE_PATTERN3));
            /*System.out.println(parseStrToLocalDate("2019-12-12"));
            System.out.println(LocalDate.now().toString().substring(0, 7));*/
        }
    
        public static String StringsecondToTime(long second) {
            long days = second / 86400;//转换天数
            second = second % 86400;//剩余秒数
            long hours = second / 3600;//转换小时数
            second = second % 3600;//剩余秒数
            long minutes = second / 60;//转换分钟
            second = second % 60;//剩余秒数
            if (0 < days) {
                return days + "天" + hours + "小时" + minutes + "分钟" + second + "秒";
            } else {
                return hours + "小时" + minutes + "分钟" + second + "秒";
            }
        }
    }
  • 相关阅读:
    SqQueue(环状队列(顺序表结构))
    LinkQueue(链队列)
    CharMatch(括号匹配)
    LinkStack
    x-boot
    安装npm及cnpm(Windows)
    sourcetree跳过注册的方法
    Mysql 5.7 CentOS 7 安装MHA
    ORA-12519 ORA-12516
    lisp : set 与setq 函数
  • 原文地址:https://www.cnblogs.com/yang1314/p/14758330.html
Copyright © 2011-2022 走看看