JDK8日期时间对象

1、LocalDate、LocalTime、LocalDateTime

package demo;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @description: demo17
 * @author: liuyang
 * @create: 2021-08-30 16:21
 */
public class Demo17 {
    public static void main(String[] args) {
        // 当前日期
        LocalDate localDate = LocalDate.now();
        // 当前时间
        LocalTime localTime = LocalTime.now();
        // 当前日期+时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
        // 创建指定日期对象
        LocalDate localDate1 = LocalDate.of(2019, 8, 30);
        // 创建指定日期时间对象
        LocalDateTime localDateTime1 = LocalDateTime.of(2021, 8, 30, 16, 31, 50);
        System.out.println(localDate1);
        System.out.println(localDateTime1);
        // 一个月的第几天
        System.out.println(localDateTime1.getDayOfMonth());
        // 一周的第几天
        System.out.println(localDateTime1.getDayOfWeek());
        // 月份英文
        System.out.println(localDateTime1.getMonth());
        // 月份数字
        System.out.println(localDateTime1.getMonthValue());
        // 时间中的分钟
        System.out.println(localDateTime1.getMinute());
        // 在localDate1的基础上得到一个新的日期对象且为一个月的第18天
        LocalDate localDate2 = localDate1.withDayOfMonth(18);
        // 在localDateTime2的基础上得到一个新的日期时间对象且小时数为18
        LocalDateTime localDateTime2 = localDateTime1.withHour(18);
        System.out.println(localDate2);
        System.out.println(localDateTime2);
        // 在localDate3的基础上得到一个新的日期对象且新日期在原来日期上加1天
        LocalDate localDate3 = localDate1.plusDays(1);
        // 在localDateTime3的基础上得到一个新的日期时间对象且在原来的小时上减去4小时
        LocalDateTime localDateTime3 = localDateTime1.minusHours(4);
        System.out.println(localDate3);
        System.out.println(localDateTime3);
    }
}

2、Instant(瞬时点)

package demo;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

/**
 * @description: demo18
 * @author: liuyang
 * @create: 2021-08-30 16:54
 */
public class Demo18 {
    public static void main(String[] args) {
        // 获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        // 获取东八区对应的时间
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(instant);
        System.out.println(offsetDateTime);
        // 获取瞬时点对应的毫秒数
        long epochMilli = instant.toEpochMilli();
        System.out.println(epochMilli);
        // 某个毫秒数对应的瞬时点
        Instant instant1 = Instant.ofEpochMilli(epochMilli);
        System.out.println(instant1);
    }
}

3、日期时间的格式化和解析

package demo;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

/**
 * @description: demo19
 * @author: liuyang
 * @create: 2021-08-30 17:55
 */
public class Demo19 {
    public static void main(String[] args) {
        /**
         * 预定义的标准格式
         * DateTimeFormatter.ISO_LOCAL_DATE
         * DateTimeFormatter.ISO_LOCAL_TIME
         * DateTimeFormatter.ISO_LOCAL_DATE_TIME
         */
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
        String result = formatter.format(localDateTime);
        System.out.println(result); // 2021-08-30
        LocalDate localDate = LocalDate.parse("2021-08-30", formatter);
        System.out.println(localDate); // 2021-08-30

        DateTimeFormatter formatter1 = DateTimeFormatter.ISO_LOCAL_TIME;
        String result1 = formatter1.format(localDateTime);
        System.out.println(result1); // 17:59:23.012
        LocalTime localTime = LocalTime.parse("17:59:23.012", formatter1);
        System.out.println(localTime); // 17:59:23.012

        DateTimeFormatter formatter2 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        String result2 = formatter2.format(localDateTime);
        System.out.println(result2); // 2021-08-30T18:00:24.774
        LocalDateTime localDateTime1 = LocalDateTime.parse("2021-08-30T18:00:24.774", formatter2);
        System.out.println(localDateTime1); // 2021-08-30T18:00:24.774

        /**
         * 本地相关的格式
         * 适用于LocalDate的格式:
         * FormatStyle.FULL、FormatStyle.LONG、FormatStyle.MEDIUM、FormatStyle.SHORT
         * 适用于LocalDateTime的格式:
         * FormatStyle.LONG、FormatStyle.MEDIUM、FormatStyle.SHORT
         */
        // LocalDate开始
        LocalDate localDate1 = LocalDate.now();
        DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String result3 = formatter3.format(localDate1);
        System.out.println(result3); // 2021年8月30日 星期一

        DateTimeFormatter formatter4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        String result4 = formatter4.format(localDate1);
        System.out.println(result4); // 2021年8月30日

        DateTimeFormatter formatter5 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        String result5 = formatter5.format(localDate1);
        System.out.println(result5); // 2021-8-30

        DateTimeFormatter formatter6 = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
        String result6 = formatter6.format(localDate1);
        System.out.println(result6); // 21-8-30

        // LocalDateTime开始
        LocalDateTime localDateTime2 = LocalDateTime.now();
        DateTimeFormatter formatter7 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        String result7 = formatter7.format(localDateTime2);
        System.out.println(result7); // 2021年8月30日 下午06时37分36秒

        DateTimeFormatter formatter8 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        String result8 = formatter8.format(localDateTime2);
        System.out.println(result8); // 2021-8-30 18:38:20

        DateTimeFormatter formatter9 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String result9 = formatter9.format(localDateTime2);
        System.out.println(result9); // 21-8-30 下午6:39

        DateTimeFormatter formatter10 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String result10 = formatter10.format(LocalDateTime.now());
        System.out.println(result10); // 2021-08-30 18:50:07
        LocalDateTime localDateTime3 = LocalDateTime.parse("2021-08-30 18:49:39", formatter10);
        System.out.println(localDateTime3); // 2021-08-30T18:49:39
    }
}

 

posted @ 2021-08-30 16:54  牛牛的自留地  阅读(57)  评论(0编辑  收藏  举报