LocalDate、LocalTime、LocalDateTime 类的实 例是不可变的对象,分别表示使用 ISO-8601日 历系统的日期、时间、日期和时间。

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

直接来看代码吧。

同时新的日期API 也解决了旧日期API在线程中的问题,

package NewTimeP;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters; public class NewTime { public void test() {
//LocalDate LocalTime LocalDateTime
//上面三个是我们可以看得懂的时间,其操作方法是一样的,这里只写一下LocalDateTime的使用方法
LocalDateTime localDateTime = LocalDateTime.now();//得到现在的时间
System.out.println("localDateTime = " + localDateTime);
//localDateTime = 2017-11-30T09:28:23.564 //按照指定的时间创建新的对象
LocalDateTime of = LocalDateTime.of(2016, 12, 12, 12, 12, 12, 23);
System.out.println("of = " + of);
//of = 2016-12-12T12:12:12.000000023 //在原有基础上加上两年,其他操作时一样的,区别一下LocalDate和LocalTime,他们是各自分别只管年月日和时分秒,所以对应的就只有操作年月日或时分秒的方法
LocalDateTime localDateTime1 = of.plusYears(2);
System.out.println("localDateTime1 = " + localDateTime1);
//localDateTime1 = 2018-12-12T12:12:12.000000023 //在原有基础上减去两年
LocalDateTime localDateTime2 = of.minusYears(2);
System.out.println("localDateTime2 = " + localDateTime2);
//localDateTime2 = 2014-12-12T12:12:12.000000023 //得到各种年月日时分秒
System.out.println(localDateTime.getYear());
System.out.println(localDateTime.getMonth());//这里是得到了Month的对象,可以对其进行操作的
System.out.println(localDateTime.getMonthValue());//这里就只是返回一个int值
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getHour());
System.out.println(localDateTime.getMinute());
System.out.println(localDateTime.getSecond());
} public void test1() {
//Instant 时间戳,以1970年一月一日00:00:00到指定时间的毫秒值
Instant instant = Instant.now();//默认获取的是UTC时区时间,世界协调时间,
System.out.println(instant);//2017-11-30T01:36:45.315Z //由于UTC时区与我们的现在的时间相差八个小时,那么我们如果想得到现在的时间就必须做一下偏移量的运算。
//下面的意思是把当前的UTC时区的时间做了八个小时的偏移量运算后返回一个偏移量的时间对象
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println("offsetDateTime = " + offsetDateTime);//offsetDateTime = 2017-11-30T09:38:59.003+08:00 //得到毫秒值
System.out.println(instant.toEpochMilli());//1512006123574 //我们可以看到结果 是在元年上进行添加的操作的!下面的意思是在1970年1月1日00:00:00上加了一秒!
Instant instant1 = Instant.ofEpochSecond(1);
System.out.println("instant1 = " + instant1);//instant1 = 1970-01-01T00:00:01Z
} public void test2() throws InterruptedException {
//Duration:计算两个时间之间的间隔的
Instant now = Instant.now();
Thread.sleep(500);
Instant now1 = Instant.now();
Duration between = Duration.between(now, now1);
System.out.println(between.toMillis());//500 这里需要提醒的是 Duration中的得到各种时间的方法是不一样的,有to啥啥啥还有 get啥啥啥,自己找找吧 LocalTime localtime = LocalTime.now();
Thread.sleep(50);
LocalTime now2 = LocalTime.now();
System.out.println(Duration.between(localtime, now2).toMillis());//51 在这的到了Duration也可以进行相加减日期的操作!
} public void test3() throws InterruptedException { //Period计算两个日期之间的间隔
LocalDate of = LocalDate.of(2015, 1, 1);
LocalDate now = LocalDate.now();
Period between = Period.between(of, now);
System.out.println("between = " + between);//between = P2Y10M29D 表示P 两年Y十个月M二十九天D
System.out.println("between = " + between.getYears());//between = 2
System.out.println("between = " + between.getDays());//between = 29
System.out.println("between = " + between.toTotalMonths());//相差三十四个月 between = 34 } public void test4() {
//TemporalAdjuster 时间校正器
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime = " + localDateTime);//localDateTime = 2017-11-30T10:01:58.836 //指定这个月中的天数
LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10);
System.out.println("localDateTime1 = " + localDateTime1);//localDateTime1 = 2017-11-10T10:01:58.836
//指定一年中的第几天 那就是 一月一号啊
LocalDateTime localDateTime2 = localDateTime.withDayOfYear(1);
System.out.println("localDateTime2 = " + localDateTime2);//localDateTime2 = 2017-01-01T10:01:58.836 //下一个周日
LocalDateTime with = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println("with = " + with);//with = 2017-12-03T10:08:23.938 //自定义校正器
//with里面是一个函数式接口,这时候就可以使用Lambda表达式
LocalDateTime with1 = localDateTime.with((e) -> {
LocalDateTime e1 = (LocalDateTime) e;//强转一下
DayOfWeek dayOfWeek = e1.getDayOfWeek();//获取今天是周几
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {//如果是周五的话,那么就加上三天,加上后就是下周周一的日期
return e1.plusDays(3);
} else if (dayOfWeek.equals(DayOfWeek.THURSDAY)) {//如果是周四的话,那么就加上四天,加上后就是下周周一的日期
return e1.plusDays(4);
}
return null;//其实不应该这么返回的,懒得写了,
});
System.out.println(with1);//2017-12-04T10:17:05.773 周一周一
} public void test5() {
//DateTimeFormatter 格式化日期与时间的。
//在这个类中其实已经提供了好多已经定义好的格式化标准
DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE; LocalDateTime dateTime = LocalDateTime.now(); String format = isoDate.format(dateTime);
System.out.println("format = " + format);//format = 2017-11-30 //也可以自己定义
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String format1 = dateTimeFormatter.format(dateTime);
System.out.println(format1);//20171130103756 //当然如果我们拿到了特定的日期时间格式也可以解析出原来的时间;
//意思就是用一个实例对象去调用parse方法,第一个参数为日期字符串,第二个就是解析的格式
LocalDateTime parse = LocalDateTime.now().parse(format1, dateTimeFormatter);
System.out.println(parse);//2017-11-30T10:40:39
}
@Test
public void test6(){
//时区 ZonedDate ZonedTime ZonedDateTime
LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println("now = " + now);//now = 2017-11-30T10:49:40.584 LocalDateTime now1 = LocalDateTime.now();
ZonedDateTime zonedDateTime = now1.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println("zonedDateTime = " + zonedDateTime);//zonedDateTime = 2017-11-30T10:51:02.094+08:00[Asia/Shanghai]
}
}

最新文章

  1. JSON数据的使用
  2. CSS标签
  3. 解决PKIX:unable to find valid certification path to requested target 的问题
  4. 使用Obsolete特性来标记方法过时或弃用
  5. nginx js、css多个请求合并为一个请求(concat模块)
  6. Linux设备模型分析之kset(基于3.10.1内核)
  7. 基于Hadoop 2.2.0的高可用性集群搭建步骤(64位)
  8. 基于Bootstrap的步骤引导html页面
  9. JDBC进阶
  10. base64_encode与base64_decode
  11. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十四)
  12. react 源码之setState
  13. 关于this的理解
  14. Android数据传递,使用广播BroadcastReceiver;
  15. flask你一定要知道的上下文管理机制
  16. spring配置文件中xsd引用问题
  17. Oceanus:美团HTTP流量定制化路由的实践
  18. 杭电 HDU 1031 Design T-Shirt
  19. 2019年华南理工大学程序设计竞赛(春季赛) B 修仙时在做什么?有没有空?可以来炼丹吗?(思维建图搜索)
  20. 快速了解jquery

热门文章

  1. asp.net core项目 Nlog直接写入集群ElasticSearch的配置方法
  2. Oracle11g R2客户端安装图文详解过程
  3. Spring cloud微服务安全实战-8-1课程总结
  4. h5上拉加载更多
  5. Navicat 8 For Mysql 数据库的导出与加载
  6. select2的使用
  7. 【Linux文件目录】的一点小结
  8. TCP粘包和拆包的定义,产生的原因以及解决方案
  9. 04 Mybatis 框架的环境搭建及入门案例
  10. [转帖]AMD Zen霄龙中国版:海光x86拿下加解密全球第一