LocalDate、 LocalTime、 LocalDateTime以及ZonedDate、 ZonedTime、 ZonedDateTime相关操作

目录

 

引言

LocalDate、 LocalTime、 LocalDateTime

Instant 时间戳

Duration 和 Period

Duration:用于计算两个“时间”间隔

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

DateTimeFormatter

ZonedDate、 ZonedTime、 ZonedDateTime


引言

LocalDate、 LocalTime、 LocalDateTime 是java8中全新的时间API,三个类的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息
 

LocalDate、 LocalTime、 LocalDateTime

三个使用同样的方法获取当前数据

//获取当前时间
LocalTime time = LocalTime.now();
//获取当前日期
LocalDate date = LocalDate.now();
//获取当前日期和时间
LocalDateTime dateTime = LocalDateTime.now();

三个使用同样的方法指定数据

LocalTime time = LocalTime.of(8, 10);
System.out.println(time);//08:10
LocalDate date = LocalDate.of(2018, 12, 24);
System.out.println(date);//2018-12-24
LocalDateTime dateTime = LocalDateTime.of(2018, 12, 12, 12, 12, 12);
System.out.println(dateTime);//2018-12-12T12:12:12

其他操作(对当前日期/时间操作)

//加减操作以及获取具体的值
LocalDateTime date = LocalDateTime.of(2018, 12, 12, 12, 12, 12);
LocalDateTime dateTime = date.plusYears(2);// +两年
System.out.println(dateTime);// 2020-12-12T12:12:12
LocalDateTime dateTime1 = date.minusMonths(10);// -10月
System.err.println(dateTime1);// 2018-02-12T12:12:12
System.out.println(date.getYear());// 2018
System.out.println(date.getMonthValue());// 12
System.out.println(date.getDayOfMonth());// 12
System.out.println(date.getHour());// 12
System.out.println(date.getMinute());// 12
System.out.println(date.getSecond());// 12



//比较两个localdate对象
LocalDate localDate = LocalDate.of(2018, 2, 12);
LocalDate localDate2 = LocalDate.of(2018, 11, 23);
boolean flag = localDate.isAfter(localDate2);
boolean flag1 = localDate.isBefore(localDate2);
System.out.println(flag);//false
System.out.println(flag1);//true

//判断是否是闰年
LocalDate localDate = LocalDate.now();
boolean flag = localDate.isLeapYear();
System.out.println(flag);//false

Instant 时间戳


用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算
 

//java7之前获取时间戳方式
long time = System.currentTimeMillis();
System.out.println(time);//1561599260726
//java8时间戳获取
Instant instant = Instant.now();//默认是UTC时区
System.out.println(instant);//2019-06-27T01:42:19.578Z
//带偏移量运算
OffsetDateTime odTime=instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(odTime);//2019-06-27T09:42:19.578+08:00
//获取毫秒显示的格式
long time1= instant.toEpochMilli();
System.out.println(time1);//1561599873015

Duration 和 Period


Duration:用于计算两个“时间”间隔

//1.如果时间是时间戳的方式(计算机读的方式),获取两个时间间隔
Instant instant1 = Instant.now();
try {
	Thread.sleep(100);
} catch (InterruptedException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
Instant instant2 = Instant.now();
//比较之后得到的是一个Duration对象,然后调用对象相应的方法
Duration duration = Duration.between(instant1, instant2);
System.out.println(duration.getSeconds());
System.out.println(duration.toMillis());
// 2.如果时间是时间方式(用户读的方法是),获取两个时间间隔
LocalTime localTime1 = LocalTime.of(12, 4, 33);
LocalTime localTime2 = LocalTime.of(12, 5, 45);
Duration duration1 = Duration.between(localTime1, localTime2);
System.out.println(duration1.getSeconds());// 72
System.out.println(duration1.toMinutes());// 1
		


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

LocalDate localDate = LocalDate.now();
LocalDate localDate2 = LocalDate.of(2018, 1, 12);
Period period=Period.between(localDate, localDate2);
System.out.println(period.getDays());

DateTimeFormatter

 

日期 时间格式化类,

//1.使用DateTimeFormatter默认提供好的格式对时间格式化,如DateTimeFormatter.ISO_DATE;
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.format(formatter));
//2.自定义时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd");
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.format(formatter));

 

ZonedDate、 ZonedTime、 ZonedDateTime
 

//查看java8中支持的时区有哪些
Set<String> strings = ZoneId.getAvailableZoneIds();
strings.forEach(System.out::println);

部分结果

//获取一个指定的时区
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
//获取指定时区的当前时间
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
System.out.println(localDateTime);
// 获取带时区的时间和日期
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
ZonedDateTime localDateTime2 = localDateTime.atZone(zoneId);
System.err.println(localDateTime2);//2019-06-27T10:44:53.672+08:00[Asia/Shanghai]

可以关注小猿微信公众号 java一号 查看更多java资源干货,更重要的是平时可以互相交流,交个朋友

文章首发地址:www.javayihao.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序三两行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值