首页 技术 正文
技术 2022年11月18日
0 收藏 647 点赞 2,615 浏览 2158 个字

原先的时间 api  大部分已经过时了

Date构造器 需要传入年月日  但是对时间的加减操作比较麻烦

Calenda  加减比较方便

使用 LocalDate、 LocalTime、 LocalDateTime


LocalDate、 LocalTime、 LocalDateTIme 类的实例是不可变的对象。分别使用ISO-8601日历系统的日期、时间、日期和时间。他们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。

他们的使用方式一一模一样,但是专一程度不一样。专门表示日期,专门表示时间,表示时间好日期

加减操作时候,有很多API可以使用的。根据情况场景自己选择。

加法操作

Java8的时间日期API

减法操作:

Java8的时间日期API

 @Test
public void test1(){
//方式一
LocalDateTime dateTime1 = LocalDateTime.now();
System.out.println(dateTime1); //方式二
LocalDateTime dateTime2 = LocalDateTime.of(2018, 05, 21, 13, 22, 33);
System.out.println(dateTime2); //获取的操作
System.out.println( dateTime1.getYear());
System.out.println(dateTime1.getHour());
}

Java8的时间日期API

以上是人读的,下面是计算机读的:

Instant: 时间戳

Unix元年, 1970年1月1日00:00:00 到某个时间之间的毫秒值

   @Test
public void test(){
Instant now = Instant.now(); //默认是获取的 UTC 时区
System.out.println(now); OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));//相差8个时区的情况
System.out.println(offsetDateTime);
//各种属性值的获取
System.out.println(now.getEpochSecond());
//1000s
Instant instant = Instant.ofEpochSecond(1000); //从unix时间开始加1000秒
System.out.println(instant);
}

Java8的时间日期API

补充:

API获取个各种属性

Java8的时间日期API

计算两个时间的间隔:

Duration: 计算两个时间之间的间隔

Period: 计算两个日期之间的间隔

   @Test
public void test3() throws InterruptedException {
Instant now1 = Instant.now();
Thread.sleep(1000);
Instant now2 = Instant.now();
//计算两个时间戳的间隔
Duration between = Duration.between(now1, now2);
System.out.println(between.getSeconds());

Java8的时间日期API

这块儿API很多:

Java8的时间日期API

duration也有很多API可以调用的!可以做加减运算!

 @Test
public void test3() throws InterruptedException {
Instant now1 = Instant.now();
Thread.sleep(1000);
Instant now2 = Instant.now();
//计算两个时间戳的间隔
Duration between = Duration.between(now1, now2);
System.out.println(between.getSeconds()); LocalTime now3 = LocalTime.now();
Thread.sleep(1000);
LocalTime now4 = LocalTime.now();
System.out.println(Duration.between(now3, now4).toMillis());
}

Java8的时间日期API

日期的间隔:

 @Test
public void test4(){
LocalDate of = LocalDate.of(2016, 2, 5);
LocalDate now = LocalDate.now();
Period between = Period.between(now, of);
System.out.println(between);
System.out.println(between.getYears());
System.out.println(between.getMonths());
}

Java8的时间日期API

获取当前月的第一天和最后一天

LocalDateTime date = LocalDateTime.now();
LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());
LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(“firstday:” + firstday);
System.out.println(“lastDay:” + lastDay);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902