淘宝支持做微交易网站吗,游戏网页链接,乾安网站建设公司电话,大同住房和城乡和建设网站Created: January 19, 2024 2:31 PM
1、旧版时间API的缺点
在java1.0中使用java.utol.Date类表示时间#xff0c;该类存在很多问题#xff0c;例如时间零点是1900年1月0日#xff0c;例如想到表示2024年1月19日就需要如下定义#xff0c;此外Date也无法表示时间、包含默认…Created: January 19, 2024 2:31 PM
1、旧版时间API的缺点
在java1.0中使用java.utol.Date类表示时间该类存在很多问题例如时间零点是1900年1月0日例如想到表示2024年1月19日就需要如下定义此外Date也无法表示时间、包含默认的CST时区
Date today new Date(124, 0, 19);
//输出Fri Jan 19 00:00:00 CST 2024在java1.1版本推出了calendar类摒弃了Date中一些方法这有了些改观但仍然不尽人意例如月份还是从1开始。同时这两个类以及常用的DateFormat类都是可变类设计在多线程环境下都存在线程安全问题。
2、Java8中的时间API
因此在java8中推出了一套新的时间API下图为新API中常用的一些类 LocalDate用于表示日期
LocalTime用于表示时间
LocalDateTime用于表示日期时间
Instant表示时间戳可精确到纳秒
Temporal上述4个类的上级接口定义通用操作
Period用于表示日期间隔
Duration用于表示时间间隔
DateTimeFormatter代替原来的DateFormat
2.1 LocalDate的常用方法
设置时间LocalDate.atAttribute()
修改某个属性LocalDate.withAttribute()
修改某个属性LocalDate.getAttribute()
加减计算LocalDate.plusAttribute()、LocalDate.minusAttribute()
//创建日期 2024/1/19
LocalDate today LocalDate.of(2024, 1, 19);
LocalDate now LocalDate.now();
//加一天 2024/1/20
LocalDate todayAdd LocalDate.from(today).plusDays(1);
//添加时间 2024/1/19 1:2:3
LocalDateTime dateTime today.atTime(1, 2, 3);
//修改日期 2022/1/19
LocalDate timeWithYear today.withYear(2022);
//获取日期 19
int dayOfYear today.getDayOfYear();2.2 LocalTime的常用方法和LocalDate非常相似
//创建时间 17:00:00
LocalTime current LocalTime.of(17, 0, 0);
LocalTime now LocalTime.now();
//加一分钟 17:01:00
LocalTime currentAdd LocalTime.from(current).plusMinutes(1);
//添加日期 2024/1/19 17:00:00
LocalDateTime dateTime current.atDate(LocalDate.now());
//修改日期 18:00:00
LocalTime withHour current.withHour(18);
//获取日期 17
int hour current.getHour();2.3 LocalDateTime的常用方法和上述两者非常相似因此不在做过多介绍
2.4 DateTimeFormatter
对时间格式化是一个我们常用的操作使用DateTimeFormatter 可以对LocalDateTime、LocalTime、LocalDate 进行格式化操作
//使用自带格式
DateTimeFormatter isoLocalDate DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//使用自定义格式
DateTimeFormatter myFormatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);
//格式化时间
String format LocalDateTime.now().format(isoLocalDate);
//解析时间
LocalDateTime parse LocalDateTime.parse(2024-01-19 08:11:22, myFormatter);
System.out.println(format);2.5 Instant
上述三种时间表示方法对人来讲十分方便但是对计算机来讲就不容易计算因此Instant主要用来机器计算Instant是一个从Unix元年1970/1/1开始计数的时间戳Instant 主要用于处理精确到纳秒级别的时间点适用于需要高精度时间记录、跨时区操作和时间点比较等场景。
Instant instant Instant.now(); // 当前的瞬时时间2.6 Duration和Period
Duration和Period都是表示间隔一个用于时间、一个用于日期
//时间间隔
Duration betweenLocalDateTime Duration.between(LocalDateTime.now(), LocalDateTime.now().plusDays(1));
System.out.println(betweenLocalDateTime : betweenLocalDateTime);
Duration duration Duration.ofDays(1);
System.out.println(duration);//PT24HPeriod Time 24 Hour
long seconds duration.getSeconds();//24*60*60//日期间隔
Period betweenLocalDate Period.between(LocalDate.now(), LocalDate.now().plusDays(1));
System.out.println(betweenLocalDate : betweenLocalDate);
Period period Period.ofDays(1);
System.out.println(period);//P1D Period 1 Day
int days period.getDays();//12.7 Temporal
Temporal 是上级接口定义了LocalDate、LocalTime、LocalDateTime、Instant中通用的方法常用的有 int get(TemporalField field) 该方法是Temporal 的上级接口 TemporalAccessor 中的方法可以获取例如这个月的第几天、这一年的地几个月等信息。传入一个TemporalField TemporalField 的实现类ChronoField 中有很多预先定义好的字段 //获取当前年的天数
int dayOfYear LocalDate.now().get(ChronoField.DAY_OF_YEAR);default Temporal with(TemporalAdjuster adjuster) 通过一个adjuster 修改 TemporalAdjuster 是一个函数时接口只有一个方法Temporal adjustInto(Temporal temporal); //修改日期
LocalDateTime myDate LocalDateTime.now().with(temporal - {if (temporal.get(ChronoField.DAY_OF_WEEK) saturday) {return temporal.with(ChronoField.DAY_OF_WEEK, 1);} else {return temporal.plus(1, ChronoUnit.DAYS);}});Temporal with(TemporalField field, long newValue); 通过一个TemporalField 和数值修改 //LocalDate.now() 2024/1/19
LocalDate dayOfWeek LocalDate.now().with(ChronoField.DAY_OF_MONTH, 1);
System.out.println(dayOfWeek);//2024-01-013 总结
在开发中只要使用的jdk版本是Java8以上都应该摒弃Date和Calendar转而使用Java8中新增的时间API