从“追求尽量不出错”,到正视“|

如此而已~~~

园龄:3年3个月粉丝:0关注:12

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);
}
}

本文作者:如此而已~~~

本文链接:https://www.cnblogs.com/fragmentary/p/17032189.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   如此而已~~~  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
//雪花飘落效果
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起