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());
    }
相關文章
相關標籤/搜索