• 日期时间工具类DateTimeUtil(基于Java8的LocalDateTime)


    1.格式化常量字符串

    public class TimeFormatter {
    
        //构造方法私有化:该类本身地方除外的其他地方无法实例化该类对象
        private TimeFormatter() {
        }
    
        public static final String DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    
        public static final String DATE_FORMATTER = "yyyy-MM-dd";
    
        public static final String TIME_FORMATTER = "HH:mm:ss";
    
        public static final String DATETIME_T_FORMATTER = "yyyy-MM-dd'T'HH:mm:ss";
    
    }

    2.DateTimeUtil日期时间工具类

    public class DateTimeUtil {
    
        /**
         * 获取当前时间时间戳(long)
         * @return
         */
        public static long currentTimeMillis() {
            return System.currentTimeMillis();
        }
    
        /**
         * 获取当前日期(yyyy-MM-dd)
         * @return
         */
        public static LocalDate currentLocalDate() {
            return LocalDate.now();
        }
    
        /**
         * 获取当前时间(HH:mm:ss.SSS)
         * @return
         */
        public static LocalTime currentLocalTime() {
            return LocalTime.now();
        }
    
        /**
         * 获取当前日期时间(yyyy-MM-dd'T'HH:mm:ss.SSS)
         * @return
         */
        public static LocalDateTime currentLocalDateTime() {
            return LocalDateTime.now();
        }
    
        /**
         * 获取当前日期字符串(yyyy-MM-dd)
         * @return
         */
        public static String getCurrentDateStr() {
            return DateTimeFormatter.ofPattern(TimeFormatter.DATE_FORMATTER).format(currentLocalDateTime());
        }
    
        /**
         * 获取当前时间字符串(HH:mm:ss)
         * @return
         */
        public static String getCurrentTimeStr() {
            return DateTimeFormatter.ofPattern(TimeFormatter.TIME_FORMATTER).format(currentLocalDateTime());
        }
    
        /**
         * 获取当前日期时间字符串(yyyy-MM-dd HH:mm:ss)
         * @return
         */
        public static String getCurrentDateTimeStr() {
            return DateTimeFormatter.ofPattern(TimeFormatter.DATETIME_FORMATTER).format(currentLocalDateTime());
        }
    
        /**
         * 将时间字符串转为自定义时间格式的LocalDateTime
         * @param time 需要转化的时间字符串
         * @param format 自定义的时间格式
         * @return
         */
        public static LocalDateTime convertStringToLocalDateTime(String time, String format) {
            return LocalDateTime.parse(time,DateTimeFormatter.ofPattern(format));
        }
    
        /**
         * 将LocalDateTime转为自定义的时间格式的字符串
         * @param localDateTime 需要转化的LocalDateTime
         * @param format 自定义的时间格式
         * @return
         */
        public static String convertLocalDateTimeToString(LocalDateTime localDateTime, String format) {
            return localDateTime.format(DateTimeFormatter.ofPattern(format));
        }
    
        /**
         * 将long类型的timestamp转为LocalDateTime
         * @param timestamp
         * @return
         */
        public static LocalDateTime convertTimestampToLocalDateTime(long timestamp) {
            return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp),ZoneId.systemDefault());
        }
    
        /**
         * 将LocalDateTime转为long类型的timestamp
         * @param localDateTime
         * @return
         */
        public static long convertLocalDateTimeToTimestamp(LocalDateTime localDateTime) {
            return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }
    
        /**
         * 获取LocalDateTime的最大时间的字符串格式(yyyy-MM-dd HH:mm:ss)
         * @param localDateTime
         * @return
         */
        public static String getMaxDateTime(LocalDateTime localDateTime) {
            return convertLocalDateTimeToString(localDateTime.with(LocalTime.MAX),TimeFormatter.DATETIME_FORMATTER);
        }
    
        /**
         * 获取LocalDateTime的最小时间的字符串格式(yyyy-MM-dd HH:mm:ss)
         * @param localDateTime
         * @return
         */
        public static String getMinDateTime(LocalDateTime localDateTime) {
            return convertLocalDateTimeToString(localDateTime.with(LocalTime.MIN),TimeFormatter.DATETIME_FORMATTER);
        }
    
        /**
         * 获取LocalDate的最大时间的字符串格式(yyyy-MM-dd HH:mm:ss)
         * @param localDate
         * @return
         */
        public static String getMaxDateTime(LocalDate localDate) {
            return convertLocalDateTimeToString(localDate.atTime(LocalTime.MAX),TimeFormatter.DATETIME_FORMATTER);
    
        }
    
        /**
         * 获取LocalDate的最小时间的字符串格式(yyyy-MM-dd HH:mm:ss)
         * @param localDate
         * @return
         */
        public static String getMinDateTime(LocalDate localDate) {
            return convertLocalDateTimeToString(localDate.atTime(LocalTime.MIN),TimeFormatter.DATETIME_FORMATTER);
        }
    }

    3.常见方法

    1.  adjustInto  调整指定的Temporal和当前LocalDateTime对
    2.  atOffset    结合LocalDateTime和ZoneOffset创建一个
    3.  atZone  结合LocalDateTime和指定时区创建一个ZonedD
    4.  compareTo   比较两个LocalDateTime
    5.  format  格式化LocalDateTime生成一个字符串
    6.  from    转换TemporalAccessor为LocalDateTi
    7.  get 得到LocalDateTime的指定字段的值
    8.  getDayOfMonth   得到LocalDateTime是月的第几天
    9.  getDayOfWeek    得到LocalDateTime是星期几
    10. getDayOfYear    得到LocalDateTime是年的第几天
    11. getHour 得到LocalDateTime的小时
    12. getLong 得到LocalDateTime指定字段的值
    13. getMinute   得到LocalDateTime的分钟
    14. getMonth    得到LocalDateTime的月份
    15. getMonthValue   得到LocalDateTime的月份,从1到12
    16. getNano 得到LocalDateTime的纳秒数
    17. getSecond   得到LocalDateTime的秒数
    18. getYear 得到LocalDateTime的年份
    19. isAfter 判断LocalDateTime是否在指定LocalDateT
    20. isBefore    判断LocalDateTime是否在指定LocalDateT
    21. isEqual 判断两个LocalDateTime是否相等
    22. isSupported 判断LocalDateTime是否支持指定时间字段或单元
    23. minus   返回LocalDateTime减去指定数量的时间得到的值
    24. minusDays   返回LocalDateTime减去指定天数得到的值
    25. minusHours  返回LocalDateTime减去指定小时数得到的值
    26. minusMinutes    返回LocalDateTime减去指定分钟数得到的值
    27. minusMonths 返回LocalDateTime减去指定月数得到的值
    28. minusNanos  返回LocalDateTime减去指定纳秒数得到的值
    29. minusSeconds    返回LocalDateTime减去指定秒数得到的值
    30. minusWeeks  返回LocalDateTime减去指定星期数得到的值
    31. minusYears  返回LocalDateTime减去指定年数得到的值
    32. now 返回指定时钟的当前LocalDateTime
    33. of  根据年、月、日、时、分、秒、纳秒等创建LocalDateTi
    34. ofEpochSecond   根据秒数(从1970-01-0100:00:00开始)创建L
    35. ofInstant   根据Instant和ZoneId创建LocalDateTim
    36. parse   解析字符串得到LocalDateTime
    37. plus    返回LocalDateTime加上指定数量的时间得到的值
    38. plusDays    返回LocalDateTime加上指定天数得到的值
    39. plusHours   返回LocalDateTime加上指定小时数得到的值
    40. plusMinutes 返回LocalDateTime加上指定分钟数得到的值
    41. plusMonths  返回LocalDateTime加上指定月数得到的值
    42. plusNanos   返回LocalDateTime加上指定纳秒数得到的值
    43. plusSeconds 返回LocalDateTime加上指定秒数得到的值
    44. plusWeeks   返回LocalDateTime加上指定星期数得到的值
    45. plusYears   返回LocalDateTime加上指定年数得到的值
    46. query   查询LocalDateTime
    47. range   返回指定时间字段的范围
    48. toLocalDate 返回LocalDateTime的LocalDate部分
    49. toLocalTime 返回LocalDateTime的LocalTime部分
    50. toString    返回LocalDateTime的字符串表示
    51. truncatedTo 返回LocalDateTime截取到指定时间单位的拷贝
    52. until   计算LocalDateTime和另一个LocalDateTi
    53. with    返回LocalDateTime指定字段更改为新值后的拷贝
    54. withDayOfMonth  返回LocalDateTime月的第几天更改为新值后的拷贝
    55. withDayOfYear   返回LocalDateTime年的第几天更改为新值后的拷贝
    56. withHour    返回LocalDateTime的小时数更改为新值后的拷贝
    57. withMinute  返回LocalDateTime的分钟数更改为新值后的拷贝
    58. withMonth   返回LocalDateTime的月份更改为新值后的拷贝
    59. withNano    返回LocalDateTime的纳秒数更改为新值后的拷贝
    60. withSecond  返回LocalDateTime的秒数更改为新值后的拷贝
    61. withYear    返回LocalDateTime年份更改为新值后的拷贝
  • 相关阅读:
    Android权限大全(链接地址整理)
    Android6.0运行时权限(基于RxPermission开源库)
    Android6.0机型上调用系统相机拍照返回的resultCode值始终等于0的问题
    使用AccessibilityService模拟点击事件失败的分析
    Android混淆打包配置总结
    okhttputils开源库的混淆配置(Eclipse)
    Android布局中的空格以及占一个汉字宽度的空格,实现不同汉字字数对齐
    java开发者大会:总结
    JAVA开发者大会:拍拍贷MQ系统原理与应用
    消息总线真的能保证幂等?
  • 原文地址:https://www.cnblogs.com/Baker-Street/p/12945896.html
Copyright © 2020-2023  润新知