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]
}
}

Java--8--新特性--新的日期API的更多相关文章

  1. 【转】Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...

  2. Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...

  3. 为什么不建议使用Date,而是使用Java8新的时间和日期API?

    Java 8:新的时间和日期API 在Java 8之前,所有关于时间和日期的API都存在各种使用方面的缺陷,因此建议使用新的时间和日期API,分别从旧的时间和日期的API的缺点以及解决方法.Java ...

  4. Atitit. visual studio vs2003 vs2005 vs2008  VS2010 vs2012 vs2015新特性 新功能.doc

    Atitit. visual studio vs2003 vs2005 vs2008  VS2010 vs2012 vs2015新特性 新功能.doc 1.1. Visual Studio2 1.2. ...

  5. Atitit.mysql 5.0 5.5  5.6 5.7  新特性 新功能

    Atitit.mysql 5.0 5.5  5.6 5.7  新特性 新功能 1. MySQL  5.6    5 大新特性1 1.1. 优化器的改进1 1.2. InnoDB 改进1 1.3. 使用 ...

  6. Atitit.mysql 5.0 5.5  5.6 5.7  新特性 新功能

    Atitit.mysql 5.0 5.5  5.6 5.7  新特性 新功能 1. MySQL  5.6    5 大新特性1 1.1. 优化器的改进1 1.2. InnoDB 改进1 1.3. 使用 ...

  7. 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient

    [源码下载] 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient 作者:webabcd 介绍重新想象 Windows 8.1 Store ...

  8. Atitit.linux 内核 新特性 新功能

    Atitit.linux 内核 新特性 新功能 1.  Linux 3.2内核新特性 2012-02-12 22:41:471 1.1. EXT4:支持更大的块2 1.2. BTRFS:更快的数据清理 ...

  9. Java 8新特性(Lambda,Stream API)

    由于最近总监要求学习Java 8的一些知识,就去网上找了 一套教程来学习学习,将学习结果做一个小的总结记录,方便以后使用: 1.Java 8的优点 2.Lambda表达式优点 2.1Lambda实例 ...

  10. Java8新特性——新一套时间API的使用

    JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: 可变性: ...

随机推荐

  1. Spring整合Redis,并配置Jedis连接池

    目录 只言片语 创建redis连接池的配置文件 单机版 spring整合redis(使用JedisPool) 项目中使用示例 集群版 spring整合redis(使用JedisCluster) 项目中 ...

  2. 软件定义网络基础---REST API概述

    一:什么是REST API REST API是北向接口的主流设计方式 API是应用程序编程接口,是预先定义好的函数,可以供应用程序或开发人员访问调用 年 Roy Thomas Fielding 的博士 ...

  3. 流行-Manifold学习理解与应用

    流行-Manifold[1]  流形,也就是 Manifold . 1. 比较好的形象理解 流形学习的观点是认为,我们所能观察到的数据实际上是由一个低维流形映射到高维空间上的,即这些数据所在的空间是“ ...

  4. Python 图片Resize.py

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 19-7-14 下午4:54 # @Author : RongT import cv2 ...

  5. 当代码上传git时,提示Repository not found The requested repository does not exist, or you do not have permission to access it. fatal: Could not read from remote repository. Please make sure you have the corre

    1.检查当前git中设置的用户名与邮箱是否与自己电脑上的一致. 看这个文件中 如果不一致,只需要把里面的内容全部复制出来添加到git(看下图位置) 这是再执行:git push -u origin m ...

  6. 用Vue2.0实现简单的分页及跳转

    用Vue2.0实现简单的分页及跳转 2018年07月26日 20:29:51 Freya_yyy 阅读数 3369    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...

  7. mysql 5.7 sql_mode设置 坑

    原文地址:https://blog.csdn.net/u012259256/article/details/56482218 1.查看sql_mode select @@sql_mode查询出来的值为 ...

  8. SpringBoot2+Druid+MyBatis+MySql实现增删改查

    1.配置pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  9. 新手配置LNMP环境教程

    回顾一下这几天自己配置LNMP环境踩得坑,希望帮助更多人 前期准备:VMtool.Linux.Nginx.Mysql.PHP.cmake 版本如下:Centos6.nginx1.6.0.mysql5. ...

  10. HDFS面试题

    hadoop节点动态上线下线怎么操作? )节点上线操作: 当要新上线数据节点的时候,需要把数据节点的名字追加在 dfs.hosts 文件中 ()关闭新增节点的防火墙 ()在 NameNode 节点的 ...