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. Qt编写小清新风格界面

    给一个朋友定制的界面,左侧有导航,左侧底部有运行+暂停+停止按钮,右侧有可伸缩面板,面板之间可以拉伸调节高度,左右两侧可以拉伸调节高度,所有的宽高和位置都保存在配置文件,下次重启立即应用,无边框标题栏 ...

  2. 实现RTSP摄像机硬盘录像机NVR网站网页微信H5直播方案EasyNVR部署问题之:ERR_CONTENT_LENGTH_MISMATCH

    背景分析 接触到EasyNVR产品的开发者都知道,EasyNVR是一套功能齐全.简洁易用的流媒体解决方案,可作为能力曾前端接入摄像头,后端接入业务系统使用,也可以作为应用层,直接修改为属于企业用户自己 ...

  3. SQL Server导入Excel文件报错

    目录 文本被截断,或者一个或多个字符在目标代码页中没有匹配项 原因 解决方法 该值违反了该列的完整性约束 空行 没有设置为允许为NULL 我以前也导入过数据,也没报错,今天再次导入数据的时候,发现了两 ...

  4. GitLab - GitLab的备份与还原

    1 - GitLab配置文件 GitLab默认的配置文件路径:/etc/gitlab/ /etc/gitlab/gitlab.rb:主配置文件,包含外部URL.仓库目录.备份目录等 /etc/gitl ...

  5. 【Python开发】PyInstaller打包Python程序

    PyInstaller是一个能将Python程序转换成单个可执行文件的程序, 操作系统支持Windows, Linux, Mac OS X, Solaris和AIX.并且很多包都支持开箱即用,不依赖环 ...

  6. 【Luogu P2765】魔术球问题

    Luogu P2765 一开始看到这道题完全想不到怎么做,绞尽脑汁也想不到怎么去构造这个网络流模型. 于是查看了多篇题解--学习了多篇题解的讲解,终于找到了思路. 本文参考了洛谷 这一道题的题意并不难 ...

  7. KMP操作大全与kuangbin kmp套题题解

    先搬运,比赛后整理 https://blog.csdn.net/vaeloverforever/article/details/82024957

  8. Deepin15.11源码安装Nginx17.5包括stream模块和njs模块

    一:先到官网下载nginx-1.17.5.tar.gz包并且解压到当前目录,解压后目录为:nginx-1.17.5: 二:下载njs源码(它没有像stream模块一样附带在了nginx源码里),因此首 ...

  9. LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38

    563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...

  10. PHP提取中英文首字母的方法(首字母索引)

    function Getzimu($str) { $str= iconv("UTF-8","gb2312", $str);//如果程序是gbk的,此行就要注释掉 ...