学习Java8中的日期类

2022-03-15  本文已影响0人  青年心路
public class Main {

    public static void main(String[] args) {
        // 获取今日日期
//        getToday();

        // 获取详细的年月日
//        getYearAndMonthAndDay();

        // 自定义日期
//        customDate();

        // 判断日期是否相等
//        checkIsEqualsTo();

        // 检查周期性日期
//        checkCycleDate();

        // 计算下一周后的日期
//        getNextWeekDate();

        // 获取前一年和后一年的日期
//        getBeforeAndNextYear();

        // 判断日期在当前之前还是之后
//        checkCurDateIsBeforeOrAfter();

        // 检查是否是闰年
//        checkLeapYear();

        // 获取当前时间(不含年月日)
//        getCurTime();

        // 时间加减计算
//        timePlusAndMinus();

        // 通过Clock时钟获取时间
//        useClock();

        // 在特定时区下的当前时间
//        useZoneDateTime();

        // 获取当前年月
//        getYearAndMonth();

        // 获取当月的天数
//        getDayCountInCurMonth();

        // 获取当前时间和指定时间的时间间隔
//        getTimeGap();

        // 通过Duration计算当前日期与指定日期的时间差
//        getTimeGapUseDuration();

        // 获取当前的时间戳
//        getTimestamp();

        // 解析指定时间
//        parseDate();

        // 字符串转时间
//        stringToDate();

        // Date与Instant互转
//        dateToInstant();

        // Date转LocalDateTime
//        dateToLocalDateTime();

        // 毫秒转LocalDateTime
//        msToLocalDateTime();

        // LocalDateTime转MS
//        localDateTimeToMS();

        // LocalDate转Date
        localDateTimeToDate();
    }

    /******************************年月日*********************************/

    /**
     * 获取今日日期
     */
    public static void getToday() {
        LocalDate today = LocalDate.now();
        System.out.println(today);
    }

    /**
     * 获取详细的年月日
     */
    public static void getYearAndMonthAndDay() {
        LocalDate today = LocalDate.now();
        System.out.println(today.getYear());
        System.out.println(today.getMonth().getValue());
        System.out.println(today.getDayOfMonth());
        System.out.println(today.getDayOfWeek().getValue());
        System.out.println(today.getDayOfYear());
    }

    /**
     * 自定义日期
     */
    public static void customDate() {
        LocalDate date = LocalDate.of(2020, 5, 21);
        System.out.println(date);
    }

    /**
     * 判断日期是否相等
     */
    public static void checkIsEqualsTo() {
        LocalDate date1 = LocalDate.now();
        LocalDate date2 = LocalDate.of(2022, 3, 15);
        System.out.println(date1.equals(date2));
    }

    /**
     * 检查周期性事件(如:生日)
     */
    public static void checkCycleDate() {
        LocalDate date1 = LocalDate.now();
        LocalDate date2 = LocalDate.of(2020, 3, 15);
        MonthDay birthday = MonthDay.of(date2.getMonth(), date2.getDayOfMonth());
        MonthDay curDay = MonthDay.from(date1);

        if (curDay.equals(birthday)) {
            System.out.println("生日快乐");
        } else {
            System.out.println("又是平凡的一天");
        }
    }

    /**
     * 计算一周后的日期
     */
    public static void getNextWeekDate() {
        LocalDate today = LocalDate.now();
        System.out.println("今天的日期:" + today);
        LocalDate nextWeekDate = today.plus(1, ChronoUnit.WEEKS);
        System.out.println("一周后的日期:" + nextWeekDate);
    }

    /**
     * 计算前一年和下一年的日期
     */
    public static void getBeforeAndNextYear() {
        LocalDate today = LocalDate.now();
        LocalDate beforeYear = today.minus(1, ChronoUnit.YEARS);
        System.out.println(beforeYear);
        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        System.out.println(nextYear);
    }

    /**
     * 判断当前日期相对于另一个日期是早还是晚
     */
    public static void checkCurDateIsBeforeOrAfter() {
        LocalDate today = LocalDate.now();
        LocalDate tomorrow = today.plusDays(1);
        if (tomorrow.isAfter(today)) {
            System.out.println("之后的日期: " + tomorrow);
        }

        LocalDate yesterday = today.minusDays(1);
        if (yesterday.isBefore(today)) {
            System.out.println("之前的日期:" + yesterday);
        }
    }

    public static void checkLeapYear() {
        LocalDate today = LocalDate.now();
        if (today.isLeapYear()) {
            System.out.println("当前是闰年");
        } else {
            System.out.println("当前是平年");
        }
    }

    /******************************时分秒*********************************/

    /**
     * 获取当前时间(不含年月日)
     */
    public static void getCurTime() {
        LocalTime time = LocalTime.now();
        System.out.println(time);
    }

    /**
     * 时间加减计算
     */
    public static void timePlusAndMinus() {
        LocalTime now = LocalTime.now();
        System.out.println("三小时后:" + now.plusHours(3));
        System.out.println("三小时前:" + now.minusHours(3));
    }

    /**
     * Clock时钟的使用,用于获取不同时区下的时间
     */
    public static void useClock() {
        Clock clock = Clock.systemUTC();
        System.out.println("clock: " + clock.instant());

        Clock defaultClock = Clock.systemDefaultZone();
        System.out.println("default clock: " + defaultClock.instant());
    }

    /**
     * 通过ZoneDateTime表示某时区下的时间
     */
    public static void useZoneDateTime() {
        ZoneId newYork = ZoneId.of("America/New_York");
        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime dateAndTimeInNewYork  = ZonedDateTime.of(localDateTime, newYork);
        System.out.println("在特定地区的当前日期和时间:" + dateAndTimeInNewYork);
    }

    /******************************年月*********************************/

    /**
     * 获取当前年月
     */
    public static void getYearAndMonth() {
        YearMonth curYearAndMonth = YearMonth.now();
        System.out.println(curYearAndMonth);
    }

    /**
     * 获取当月天数
     */
    public static void getDayCountInCurMonth() {
        YearMonth yearMonth = YearMonth.now();
        System.out.println(yearMonth.lengthOfMonth());
    }

    /**
     * 通过Period计算当前时间和某个时间的间隔
     *
     * getMonths()和getDays()的值
     * 5.10/5.1:  1,9
     * 5.10/4.1:1,9  (月份和天数相加才是真正相差的数)
     * 6.10/4.1:2,9  (月份和天数相加才是真正相差的数)
     */
    public static void getTimeGap() {
        LocalDate today = LocalDate.now();
        LocalDate startLearn = LocalDate.of(2022, 2, 7);
        Period period = Period.between(today, startLearn);
        System.out.println("时间间隔:" + period.getMonths());
    }

    /**
     * 通过Duration计算时间差值
     */
    public static void getTimeGapUseDuration() {
        LocalDateTime today = LocalDateTime.now();
        LocalDateTime beforeWeek = today.minusWeeks(1);
        Duration duration = Duration.between(today, beforeWeek);
        System.out.println(duration.toHours());
    }

    /******************************Instant时间戳*********************************/

    /**
     * 获取当前时间的时间戳
     */
    public static void getTimestamp() {
        long timestamp1 = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        System.out.println(timestamp1);

        long timestamp2 = Instant.now().toEpochMilli();
        System.out.println(timestamp2 + " " + new Date().getTime());
    }

    /******************************使用指定的格式解析时间*********************************/

    /**
     * 通过指定的格式解析时间
     */
    public static void parseDate() {
        String curDate = "20220315";
        LocalDate localDate = LocalDate.parse(curDate, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(localDate);
    }

    /**
     * 字符串互转日期类型
     */
    public static void stringToDate() {
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String str = date.format(formatter1);
        System.out.println("日期转换为字符串:" + str);

        DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime date2 = LocalDateTime.parse(str, format2);
        System.out.println("日期类型:" + date2);
    }

    /**
     * Date和Instant互转
     */
    public static void dateToInstant() {
        // Instant转Date
        Date date = Date.from(Instant.now());
        // Date转Instant
        Instant instant = date.toInstant();
        System.out.println(date);
        System.out.println(instant);
    }

    /**
     * Date转LocalDateTime
     */
    public static void dateToLocalDateTime() {
        LocalDateTime localDateTime = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println(localDateTime);
    }

    /**
     * 毫秒转LocalDateTime
     */
    public static void msToLocalDateTime() {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(
                Instant.ofEpochMilli(new Date().getTime()), ZoneId.systemDefault()
        );
        System.out.println(localDateTime);
    }

    /**
     * LocalDateTime转毫秒
     */
    public static void localDateTimeToMS() {
        long milli = LocalDateTime.of(2020, 3, 1, 0, 0, 0)
                .toInstant(ZoneOffset.of("+8")).toEpochMilli();
        System.out.println(milli);
    }

    /**
     * LocalDate转Date
     */
    public static void localDateTimeToDate() {
        LocalDate now = LocalDate.now();
        Date date = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println(date);
    }
}
上一篇下一篇

猜你喜欢

热点阅读