JavaImprove--Lesson04--LocalDateTime,ZoneId,Instant,DateTimeFormatter

一.LocalDateTime

LocalDateTime是JDK8的新时间特性,它解决了Date类和Calender类的很多不足,如使用不方便,线程不安全,以及获取时间戳只能拿到毫秒而不能拿到纳秒等问题

使用LocalDateTime可以获得的时间有:年-月-日,时:分:秒,纳秒

而LocalDate和LocalTime可以组合成LocalDateime,反之,LocalDateTime也可以分解成为它们两个

LocalDate封装的是 :年-月-日

LocalTime封装的是:时:分:秒,纳秒

LocalDateTime封装了二者,所以也是开发中使用的最多的,其内部的方法使用也是差不多的,可以类举,所以我们主要看LocalDateTime

LocalDateTime 是 Java 8 中引入的一个类,属于 java.time 包的一部分,是用来表示没有时区的日期和时间的。LocalDateTime 是不可变的,也就是说一旦创建了 LocalDateTime 对象,就不能修改它的值。

LocalDateTime 提供了许多有用的方法来操作日期和时间,例如获取日期和时间的各个部分(年、月、日、小时、分钟等),计算两个日期之间的差异,以及将日期和时间格式化为字符串等。

获得LocalDateTime

//获得LocalDateTime对象
LocalDateTime NowTime = LocalDateTime.now();
System.out.println(NowTime); //2024-01-07T16:58:40.024

 

获得相关日期的方法:

//获得年
int year = nowTime.getYear();
System.out.println(year);//2024
//获得月
Month month = nowTime.getMonth();
System.out.println(month.getValue());//1
//获得日
int dayOfYear = nowTime.getDayOfYear();
System.out.println(dayOfYear);//7
//获得星期
int value = nowTime.getDayOfWeek().getValue();
System.out.println(value);//7
//获得小时
int hour = nowTime.getHour();
System.out.println(hour);//17

 

with----():修改日期的,并且返回一个新的对象

//修改年
LocalDateTime newYear = nowTime.withYear(2028);
System.out.println(newYear.getYear()); //2028
//修改月
LocalDateTime newMonth = nowTime.withMonth(7);
System.out.println(newMonth.getMonth().getValue());//7
//修改分钟
LocalDateTime newMinute = nowTime.withMinute(50);
System.out.println(newMinute.getMinute());//50

 

plus----():增加日期,对某个日期进行新增,并且返回一个新的对象

//增加8个月
LocalDateTime newMonth = nowTime.plusMonths(8);
System.out.println(newMonth.getMonth().getValue());// 9 = 1 + 8
//增加3小时
LocalDateTime newHour = nowTime.plusHours(3);
System.out.println(newHour.getHour()); //20 = 17 + 3

 

minus----():减少日期,对某个日期进行减少,并且返回一个新的对象

//日期减少5天
LocalDateTime newDay = nowTime.minusDays(5);
System.out.println(newDay.getDayOfYear()); // 2 = 7-5
//日期减少5s
LocalDateTime newSecond = nowTime.minusSeconds(5);
System.out.println(newSecond.getSecond());// 17 = 22-5

 

equlas,isBefore,isAfter:时间对比方法

//获得LocalDateTime对象
LocalDateTime nowTime = LocalDateTime.now();
//新增1s
LocalDateTime newSecond = nowTime.plusSeconds(1);
//equals
boolean a = nowTime.equals(newSecond);
System.out.println(a); //false  nowTime 比 newSecond 少 1s 
//isAfter
boolean b = nowTime.isAfter(newSecond);
System.out.println(b);//false  nowTime 比 newSecond 少 1s,故 nowTime在newSecond之后
//isBefore
boolean c = nowTime.isBefore(newSecond);
System.out.println(c); //ture  nowTime 比 newSecond 少 1s, 故 nowTime在newSecond之后

 

二.ZoneID时区ID

时区用于Java开发中取任何地区的时间信息,当然指的是标准地区时间,也就是,中国可以取到北京,上海等地区时间,不是每个城市都可以取到,一定是标志性城市

在Java中,ZoneId 是 java.time 包中的一个类,它用于表示一个特定的时区。时区是一个地理区域,其时间标准由一个特定的时区偏移量定义。这个时区偏移量是相对于协调世界时(UTC)的偏移量。

使用 ZoneId,你可以更容易地处理与特定时区相关的日期和时间,并执行时区特定的计算和转换

获取当前时区:

//获取系统默认的时区(当前系统)
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId.getId());//Asia/Shanghai
System.out.println(zoneId);  //Asia/Shanghai

 

获取时区集合:

Set<String> zoneIds = ZoneId.getAvailableZoneIds()
System.out.println(zoneIds);//内容太多,便展示,包括所有的时区

 

将时区封装成ZoneId:

//把时区Id封装成ZoneId
ZoneId Chongqing = ZoneId.of("Asia/Chongqing");//亚洲重庆

 

获得ZoneId的时间:

//获得重庆的时间
ZonedDateTime ChongqingTime = ZonedDateTime.now(Chongqing);
System.out.println(ChongqingTime);

 

获得世界标准时间----本次子午线时间:

System.out.println(ChongqingTime);//2024-01-07T09:54:36.623Z
//获得世界标准时间--本初子午线
ZonedDateTime utc = ZonedDateTime.now(Clock.systemUTC());
System.out.println(utc); //2024-01-07T09:54:36.623Z

 

获得系统当前时区时间:

//获取系统的默认时间
ZonedDateTime nowTime = ZonedDateTime.now();
System.out.println(nowTime);

 

时区时间也是支持LocaDateTime类相关的方法的:

//在时区时间的基础上新增8年
ZonedDateTime addEightYear = nowTime.plusYears(8);
System.out.println(addEightYear);
//在时区时间的基础上减少10天
ZonedDateTime minusTenDay = nowTime.minusDays(10);
System.out.println(minusTenDay);

 

三.Instant

时刻,时间戳,它代表的是某一时刻到1970年1月1日0:0:0的毫秒数,不足1毫秒的用纳秒代替

1毫秒 = 1000微秒

1微秒=1000纳秒

在Java中,Instant 是 java.time 包中的一个类,它用于表示一个特定的时间点,通常与UTC时间关联。与 Date 类相比,Instant 提供了更精确的时间表示,因为它不包含时区或夏令时调整的信息。

Instant 类包含表示时间的基本属性,如年、月、日、小时、分钟和秒,但这些属性通常只用于计算时间差,而不是用于表示具体的时间点。

获取Instant对象:

//获取Instant对象
Instant now = Instant.now();

 

获取毫秒和纳秒:

 //获取毫秒
 long epochSecond = now.getEpochSecond();
 System.out.println(epochSecond);//1704622124
 //获取纳秒
 int nano = now.getNano();
 System.out.println(nano);//825000000

 

equlas,isBefore,isAfter:时间对比方法

//开始时间戳
Instant start = Instant.now();
Thread.sleep(5);//休息5毫秒
//结束时间戳
Instant end = Instant.now();
//equals
boolean equals = start.equals(end);
System.out.println(equals); //false
//isAfter
boolean after = start.isAfter(end);
System.out.println(after); //false
//isBefore
boolean before = start.isBefore(end);
System.out.println(before);//true

 四.DateTimeFormatter

 时间格式化器

是JDK8新出的时间格式类,用来代替SimpleDateFormat类的,是线程安全的类

DateTimeFormatter 是 Java 8 及其以后版本中 java.time 包的一部分,用于格式化和解析日期和时间。这个类提供了一种灵活的方式来表示日期和时间的格式

 创建日期格式化器:

// 创建日期格式化器对象
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

 

格式化时间:

//获取当前系统时间
LocalDateTime now = LocalDateTime.now();
//格式化时间
String time = pattern.format(now);
System.out.println(time);//2024年01月07日 20:27:31

 

利用LocacDateTime对象的 format方法格式化时间:

//获取当前系统时间
LocalDateTime now = LocalDateTime.now();
//格式化时间
String time = now.format(pattern);
System.out.println(time);//2024年01月07日 20:27:31

 

解析时间:

// 创建日期格式化器对象
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
//解析时间
String dat = "2024年12月12日 12:12:10";
//字符串格式转时间格式
LocalDateTime time = LocalDateTime.parse(dat, pattern);
System.out.println(time);

 

注意:解析时间的时候一定要注意占位,如果格式化器上的是 mm :代表两位数的分钟,那么,字符串中就必须是两位数如果不满两位数就用0补齐,07,08等

不然就解析不成功

period 

 一段时期

可以计算两个LocalDate对象相差的年,月,日

在Java中,Period 类是 java.time 包的一部分,用于表示两个日期之间的时间跨度。这个类主要用于处理日期间隔,并且不包含时间信息。

//两个时间对象
LocalDate y1 = LocalDate.of(2002, 11, 4);
LocalDate y2 = LocalDate.of(2022, 12, 25);
//创建period对象,将两个时间对象放入
Period period = Period.between(y1, y2);
System.out.println(period.getYears());//年 20
System.out.println(period.getMonths());//月 1
System.out.println(period.getDays());//日  21

 

 Duration

可以用于计算两个时间对象相差的天数,小时数,分数,秒数,纳秒数

支持laoclTime,LocalDateTime,Instant等

Java的 Duration 类是 java.time 包的一部分,用于表示一个时间段的长度,以秒和纳秒为单位。这个类主要用于处理时间间隔,并且可以用于执行各种时间相关的操作

//两个时间对象
LocalDateTime y1 = LocalDateTime.of(2002, 03, 05, 8, 2, 12);
LocalDateTime y2 = LocalDateTime.of(2002, 03, 05, 10, 14, 20);
//创建period对象,将两个时间对象放入
Duration duration = Duration.between(y1, y2);
System.out.println(duration.toDays()); //天  0
System.out.println(duration.toHours()); //小时 2
System.out.println(duration.toMinutes()); //分钟 132
System.out.println(duration.getSeconds());//秒 7298
System.out.println(duration.toNanos());//纳秒 7928000000000

 

posted @ 2024-01-07 21:08  回忆也交给时间  阅读(8)  评论(0编辑  收藏  举报