zoukankan      html  css  js  c++  java
  • jdk8处理时间

        对当前时间格式化:
         public static long getCurrentTimeMillis(String pattern) {
            return Long.valueOf(toString(LocalDateTime.now(), pattern));
         }
         对指定日期进行格式化:
         public static LocalDate toLocalDate(String date, String pattern) {
            return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
    
         }
        public static String toString(LocalDate date, String pattern) {
           return date.format(DateTimeFormatter.ofPattern(pattern));
        }
        // 计算偏移日期
        public static LocalDate getOffsetLocalDate(LocalDate targetDate, ChronoUnit unit, long number) {
            return targetDate.plus(number, unit);
        }
    
        // 获取周一
        public static LocalDate getFirstDayOfWeek(LocalDate localDate) {
            return localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        }
    
        // 获取周日
        public static LocalDate getLastDayOfWeek(LocalDate localDate) {
            return localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        }
    
        // 获取月初
        public static LocalDate getFirstDayOfMonth(LocalDate localDate) {
            return localDate.with(TemporalAdjusters.firstDayOfMonth());
        }
    
        // 获取月末
        public static LocalDate getLastDayOfMonth(LocalDate localDate) {
            return localDate.with(TemporalAdjusters.lastDayOfMonth());
        }
    
        // 获取季初
        public static LocalDate getFirstDayOfQuarter(LocalDate localDate) {
            int month = localDate.getMonthValue();
            if (month >= 1 && month <= 3) {
                return LocalDate.of(localDate.getYear(), 1, 1);
            } else if (month >= 4 && month <= 6) {
                return LocalDate.of(localDate.getYear(), 4, 1);
            } else if (month >= 7 && month <= 9) {
                return LocalDate.of(localDate.getYear(), 7, 1);
            } else {
                return LocalDate.of(localDate.getYear(), 10, 1);
            }
    
        }
    
        // 获取季末
        public static LocalDate getLastDayOfQuarter(LocalDate localDate) {
            int month = localDate.getMonthValue();
            if (month >= 1 && month <= 3) {
                return LocalDate.of(localDate.getYear(), 3, 31);
            } else if (month >= 4 && month <= 6) {
                return LocalDate.of(localDate.getYear(), 6, 30);
            } else if (month >= 7 && month <= 9) {
                return LocalDate.of(localDate.getYear(), 9, 30);
            } else {
                return LocalDate.of(localDate.getYear(), 12, 31);
            }
        }
    
        // 获取半年初
        public static LocalDate getFirstDayOfHalfYear(LocalDate localDate) {
            int month = localDate.getMonthValue();
            if (month >= 1 && month <= 6) {
                return LocalDate.of(localDate.getYear(), 1, 1);
            } else {
                return LocalDate.of(localDate.getYear(), 7, 1);
            }
    
        }
    
        // 获取半年末
        public static LocalDate getLastDayOfHalfYear(LocalDate localDate) {
            int month = localDate.getMonthValue();
            if (month >= 1 && month <= 6) {
                return LocalDate.of(localDate.getYear(), 6, 30);
            } else {
                return LocalDate.of(localDate.getYear(), 12, 31);
            }
        }
    
        // 获取年初
        public static LocalDate getFirstDayOfYear(LocalDate localDate) {
            return localDate.with(TemporalAdjusters.firstDayOfYear());
        }
    
        // 获取年末
        public static LocalDate getLastDayOfYear(LocalDate localDate) {
            return localDate.with(TemporalAdjusters.lastDayOfYear());
        }
    
        // 计算两个日期相差天数
        public static long getIntervalDays(LocalDate localDate1, LocalDate localDate2) {
            return Math.abs(localDate1.toEpochDay() - localDate2.toEpochDay());
        }
        
    

      

  • 相关阅读:
    Atitit.500 503 404错误处理最佳实践oak
    Atitit. 解决unterminated string literal 缺失引号
    Atitit. Java script 多重多重catch语句的实现and Javascript js 异常机制
    Atitit. Dwr 抛出异常error解决方案
    Atitit.js javascript异常处理机制与java异常的转换.js exception process Voae
    Atitit.软件gui按钮and面板---通讯子系统(区)-- github 的使用....
    atitit。gui 界面皮肤以及换肤总结 java .net c++
    atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
    atitit.报表最佳实践oae 与报表引擎选型
    Atitit. 软件---多媒体区---- jmf 2.1.1 Java Media Framework 支持的格式
  • 原文地址:https://www.cnblogs.com/qinjf/p/10861069.html
Copyright © 2011-2022 走看看