Java--8--新特性--新的日期API
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的更多相关文章
- 【转】Java 8新特性(四):新的时间和日期API
Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...
- Java 8新特性(四):新的时间和日期API
Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...
- 为什么不建议使用Date,而是使用Java8新的时间和日期API?
Java 8:新的时间和日期API 在Java 8之前,所有关于时间和日期的API都存在各种使用方面的缺陷,因此建议使用新的时间和日期API,分别从旧的时间和日期的API的缺点以及解决方法.Java ...
- 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. ...
- 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. 使用 ...
- 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. 使用 ...
- 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient
[源码下载] 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient 作者:webabcd 介绍重新想象 Windows 8.1 Store ...
- Atitit.linux 内核 新特性 新功能
Atitit.linux 内核 新特性 新功能 1. Linux 3.2内核新特性 2012-02-12 22:41:471 1.1. EXT4:支持更大的块2 1.2. BTRFS:更快的数据清理 ...
- Java 8新特性(Lambda,Stream API)
由于最近总监要求学习Java 8的一些知识,就去网上找了 一套教程来学习学习,将学习结果做一个小的总结记录,方便以后使用: 1.Java 8的优点 2.Lambda表达式优点 2.1Lambda实例 ...
- Java8新特性——新一套时间API的使用
JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: 可变性: ...
随机推荐
- Python3基础 yield send 变量名= yield i
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- WebGL学习笔记(一):理解基本概念和渲染管线
WebGL 是以 OpenGL ES 2.0 为基础的 3D 编程应用接口. 渲染管线(图形流水线) 渲染管线是指将数据从3D场景转换成2D图像,最终在屏幕上显示出来的总过程.它分为几个阶段:应用阶段 ...
- C语言 字符串切割
#include <stdio.h> #include <stdlib.h> #include <string.h> /* 字符串切割函数 */ /* 知识补充: ...
- Linux下查找命令 —— find、grep、 which、 whereis、 locate
find命令 基本格式 find < path > < expression > < cmd > ''' path: 所要搜索的目录及其所有子目录.默认为当前目录. ...
- LeetCode_437. Path Sum III
437. Path Sum III Easy You are given a binary tree in which each node contains an integer value. Fin ...
- 创建slackapp prometheus告警发到slack
创建slackapp: https://blog.walterlv.com/post/slack-api-starter-incoming-webhooks.html#%E5%88%9B%E5%BB% ...
- 密码工具:KeePassXC
KeePassXC 开源免费 只支持数据库存放在本地, 跨平台 自动填充 使用浏览器插件时,软件必须启动着 KeePassXC is a community fork of KeePassX, a n ...
- 【Python学习之九】模块
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 一.模块的使用和安装模块和C语言中的头文件以及Ja ...
- 1、快速搭建后台list
@RestController @CrossOrigin public class UserController { @Resource private UserService userService ...
- C++Primer 5th Chap2 Variables and basic Types
wchar_t,char16_t,char32_t用于拓展字符集 char和signed char并不一样,由编译器决定类型char表现上述两种中的哪一种 一般long的大小和int无二,如果超过in ...