34_Java8 日期API

Java8 日期API

Date如果不格式化;输出的日期可读性差;而Java8 的时间类直接输出可读性好

Date存在线程安全问题;而Java8的时间类都是线程安全的

JDK8新增日期类:

​ LocalDate:年、月、日

​ LocalTime:时、分、秒

LocalDateTime:年、月、日、时、分、秒

​ Instant:代表的是时间戳

DateTimeFormatter:用于时间的格式化和解析

​ Duration:计算两个“时间”间隔

​ Period:计算两个“日期”间隔

LocalDateTime

1、获取LocalDateTime对象:

没有构造方法,常用下面两个静态方法获取时间:

​ public static LocalDateTime now()

​ 从默认时区中的系统时钟获取当前日期时间

​ public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)

​ 从年、月、日、小时、分钟、和秒获取LocalDateTime的实例,将纳秒设置为零

参考代码:

package com.ithiema;
import java.time.LocalDateTime;

/*
    public static LocalDateTime now()
			从默认时区中的系统时钟获取当前日期时间
	public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
			从年、月、日、小时、分钟、和秒获取LocalDateTime的实例,将纳秒设置为零
 */
public class LocalDateTimeDemo01 {
    public static void main(String[] args){
        // public static LocalDateTime now()
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        //2023-01-03T15:56:42.232014900
        /*
            T:表示时间开始,精确到纳秒
            1秒 = 1000毫秒
            1毫秒 = 1000微秒
            1微秒 = 1000纳秒
            1秒 = 1 * 10^9纳秒
         */

        //public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
        LocalDateTime of = LocalDateTime.of(2023, 1, 3, 16, 1, 42);
        System.out.println(of);
        //2023-01-03T16:01:42 此处我们只精确到了秒
    }
}
2、LocalDateTime格式化和解析:

格式化:

​ String format(DateTimeFormatter format):使用指定的格式化程序格式化此日期时间

解析:

​ static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter):

​ 使用特定格式化程序从文本字符串中获取LocalDateTime的实例

DateTimeFormatter:没看到构造方法,用下面的静态方法获取日期格式化对象

​ public static DateTimeFormatter ofPattern(String pattern):使用指定的模式创建格式化程序

参考代码:

package com.ithiema;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/*
    格式化:
		String format(DateTimeFormatter format):使用指定的格式化程序格式化此日期时间

    解析:
		static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter):
				使用特定格式化程序从文本字符串中获取LocalDateTime的实例

    DateTimeFormatter:没看到构造方法,用下面的静态方法获取日期格式化对象
		public static DateTimeFormatter ofPattern(String pattern):使用指定的模式创建格式化程序
 */
public class LocalDateTimeDemo02 {
    public static void main(String[] args){
        //一、格式化
        /*//获取LocalDateTime对象
        //String format(DateTimeFormatter format)
        LocalDateTime now = LocalDateTime.now();

        //获取DateTimeFormatter对象
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = now.format(dateTimeFormatter);

        System.out.println(format); //2023-01-03 16:21:35*/

        //使用链式编程进行改进
        //创建日期对象,调用其格式化方法,传入格式对象参数
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(format);

        //二、解析:注意格式是否一致
        //static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
//        String s = "2023-01-03 16:21:35";
        String s = "2023/01/03 16:21:35";
//        LocalDateTime parse = LocalDateTime.parse(s, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime parse = LocalDateTime.parse(s, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
        System.out.println(parse);
    }
}
posted @ 2023-01-07 10:15  如此而已~~~  阅读(18)  评论(0编辑  收藏  举报