Mannix‘s Marauders -Chris Mannix  曼尼克斯掠夺者 曼尼克斯·克里斯

“I'm the new sheriff of Red Rock.”  “我是红石镇的新任警长”

一、基础介绍

目前Java8已经实现了JSR310的全部内容。新增了java.time包定义的类表示了日期-时间概念的规则,包括instants, durations, dates, times, time-zones and periods。这些都是基于ISO日历系统,它又是遵循 Gregorian规则的。最重要的一点是值不可变,且线程安全,通过下面一张图,我们快速看下java.time包下的一些主要的类的值的格式,方便理解。

Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理。对日期与时间的操作一直是Java程序员最痛苦的地方之一。标准的 java.util.Date以及后来的java.util.Calendar一点没有改善这种情况(可以这么说,它们一定程度上更加复杂)。

这种情况直接导致了Joda-Time——一个可替换标准日期/时间处理且功能非常强大的Java API的诞生。Java 8新的Date-Time API (JSR 310)在很大程度上受到Joda-Time的影响,并且吸取了其精髓。新的java.time包涵盖了所有处理日期,时间,日期/时间,时区,时刻(instants),过程(during)与时钟(clock)的操作。在设计新版API时,十分注重与旧版API的兼容性:不允许有任何的改变(从java.util.Calendar中得到的深刻教训)。如果需要修改,会返回这个类的一个新实例。

 Java8中的时间函数包已经重新在java.time中安家。

二、具体使用

1、获取UTC

让我们用例子来看一下新版API主要类的使用方法。第一个是Clock类,它通过指定一个时区,然后就可以获取到当前的时刻,日期与时间。Clock可以替换System.currentTimeMillis()与TimeZone.getDefault()。

获取到的时间是UTC,和北京时间比这个早8个小时。如果结果是上午6点,对应的北京时间是下午两点(在小时的地方+8)

// Get the system clock as UTC offset
final Clock clock = Clock.systemUTC();
System.out.println( clock.instant() );

下面是程序在控制台上的输出:

2017-12-22T07:47:18.124Z

2、获取当地日期、时间

我们需要关注的其他类是LocaleDate与LocalTime。LocaleDate只持有ISO-8601格式且无时区信息的日期部分。相应的,LocaleTime只持有ISO-8601格式且无时区信息的时间部分。LocaleDate与LocalTime都可以从Clock中得到。

LocaleDate与LocalTim获取到的是本时区的时间//使用的场合最多,可以使用.now()直接获取,也可以使用of方法进行构造

// Get the local date and local time
final LocalDate date = LocalDate.now();
System.out.println( date ); // Get the local date and local time
final LocalTime time = LocalTime.now();
System.out.println( time ); //Creating LocalDate by providing input arguments
LocalDate firstDay_2014 = LocalDate.of(2014, 1, 1);
System.out.println("Specific Date="+firstDay_2014); //Creating LocalTime by providing input arguments
LocalTime specificTime = LocalTime.of(12,20,25,40);
System.out.println("Specific Time of Day="+specificTime);

下面是程序在控制台上的输出:

2017-12-22
15:46:32.255
Specific Date=2014-01-01
Specific Time of Day=12:20:25.000000040

3、获取当地日期和时间

LocaleDateTime把LocaleDate与LocaleTime的功能合并起来,它持有的是ISO-8601格式无时区信息的日期与时间。同样可以使用of进行构造

// Get the local date/time
final LocalDateTime datetime = LocalDateTime.now();
System.out.println( datetime );

下面是程序在控制台上的输出:

2014-04-12T11:37:52.309
2014-04-12T11:37:52.309

4、指定时区的日期时间获取

如果你需要特定时区的日期/时间,那么ZonedDateTime是你的选择。它持有ISO-8601格式具具有时区信息的日期与时间。下面是一些不同时区的例子:

// Get the zoned date/time
final ZonedDateTime zonedDatetime = ZonedDateTime.now();
final ZonedDateTime zonedDatetimeFromClock = ZonedDateTime.now( clock );
final ZonedDateTime zonedDatetimeFromZone = ZonedDateTime.now( ZoneId.of( "America/Los_Angeles" ) ); System.out.println( zonedDatetime );
System.out.println( zonedDatetimeFromClock );
System.out.println( zonedDatetimeFromZone );

下面是程序在控制台上的输出:

2014-04-12T11:47:01.017-04:00[America/New_York]
2014-04-12T15:47:01.017Z
2014-04-12T08:47:01.017-07:00[America/Los_Angeles]

5、计算日期时间间隔

使用Duration类:在秒与纳秒级别上的一段时间。Duration使计算两个日期间的不同变的十分简单。下面让我们看一个这方面的例子。

// Get duration between two dates
final LocalDateTime from = LocalDateTime.of( 2014, Month.APRIL, 16, 0, 0, 0 );
final LocalDateTime to = LocalDateTime.of( 2015, Month.APRIL, 16, 23, 59, 59 ); final Duration duration = Duration.between( from, to );
System.out.println( "Duration in days: " + duration.toDays() );
System.out.println( "Duration in hours: " + duration.toHours() );

  

上面的例子计算了两个日期2014年4月16号与2014年4月16号之间的过程。下面是程序在控制台上的输出:

Duration in days: 365
Duration in hours: 8783

6、日期时间的加减操作

package com.journaldev.java8.time;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.temporal.TemporalAdjusters; public class DateAPIUtilities { public static void main(String[] args) {
    LocalDate today = LocalDate.now();

    LocalTime nowTime = LocalTime.now();

    //Get the Year, check if it's leap year
    System.out.println("Year "+today.getYear()+" is Leap Year? "+today.isLeapYear());     //Compare two LocalDate for before and after
    System.out.println("Today is before 01/01/2015? "+today.isBefore(LocalDate.of(2015,1,1)));     //Create LocalDateTime from LocalDate
    System.out.println("Current Time="+today.atTime(LocalTime.now()));     //plus and minus operations
    System.out.println("10 days after today will be "+today.plusDays(10));
    System.out.println("3 weeks after today will be "+today.plusWeeks(3));
    System.out.println("20 months after today will be "+today.plusMonths(20));
    System.out.println("20 years after today will be "+today.plusYears(20));     System.out.println("10 days before today will be "+today.minusDays(10));
    System.out.println("3 weeks before today will be "+today.minusWeeks(3));
    System.out.println("20 months before today will be "+today.minusMonths(20));
    System.out.println("20 Hours after now will be "+nowTime.plusHours(20));
    System.out.println("20 Hours before now will be "+nowTime.minusHours(20));
} }

  输出结果是

Year 2017 is Leap Year? false
Today is before 01/01/2015? false
Current Time=2017-12-22T16:05:46.767
10 days after today will be 2018-01-01
3 weeks after today will be 2018-01-12
20 months after today will be 2019-08-22
20 years after today will be 2037-12-22
10 days before today will be 2017-12-12
3 weeks before today will be 2017-12-01
20 months before today will be 2016-04-22
20 Hours after now will be 12:05:46.767
20 Hours before now will be 20:05:46.767

7、Date/Time和String转换

再来个很常用的功能,String和Date的转换//使用的场合最多

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//时间转为字符串
LocalDateTime date =LocalDateTime.now();
String str = date.format(f);
String str = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//直接把转换格式写在参数里面,相当于两行代码
//字符串转为时间
date = LocalDateTime.parse(“2017-12-22 16:18:32”,f);
date = LocalDateTime.parse(“2017-12-22 16:18:32”,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//直接把转换格式写在参数里面,相当于两行代码
);
 

  

三、功能整合列表

public class TimeIntroduction {
public static void testClock() throws InterruptedException {
//时钟提供给我们用于访问某个特定 时区的 瞬时时间、日期 和 时间的。
Clock c1 = Clock.systemUTC(); //系统默认UTC时钟(当前瞬时时间 System.currentTimeMillis())
System.out.println(c1.millis()); //每次调用将返回当前瞬时时间(UTC)
Clock c2 = Clock.systemDefaultZone(); //系统默认时区时钟(当前瞬时时间)
Clock c31 = Clock.system(ZoneId.of("Europe/Paris")); //巴黎时区
System.out.println(c31.millis()); //每次调用将返回当前瞬时时间(UTC)
Clock c32 = Clock.system(ZoneId.of("Asia/Shanghai"));//上海时区
System.out.println(c32.millis());//每次调用将返回当前瞬时时间(UTC)
Clock c4 = Clock.fixed(Instant.now(), ZoneId.of("Asia/Shanghai"));//固定上海时区时钟
System.out.println(c4.millis());
Thread.sleep(1000);
System.out.println(c4.millis()); //不变 即时钟时钟在那一个点不动
Clock c5 = Clock.offset(c1, Duration.ofSeconds(2)); //相对于系统默认时钟两秒的时钟
System.out.println(c1.millis());
System.out.println(c5.millis());
}
public static void testInstant() {
//瞬时时间 相当于以前的System.currentTimeMillis()
Instant instant1 = Instant.now();
System.out.println(instant1.getEpochSecond());//精确到秒 得到相对于1970-01-01 00:00:00 UTC的一个时间
System.out.println(instant1.toEpochMilli()); //精确到毫秒
Clock clock1 = Clock.systemUTC(); //获取系统UTC默认时钟
Instant instant2 = Instant.now(clock1);//得到时钟的瞬时时间
System.out.println(instant2.toEpochMilli());
Clock clock2 = Clock.fixed(instant1, ZoneId.systemDefault()); //固定瞬时时间时钟
Instant instant3 = Instant.now(clock2);//得到时钟的瞬时时间
System.out.println(instant3.toEpochMilli());//equals instant1
}
public static void testLocalDateTime() {
//使用默认时区时钟瞬时时间创建 Clock.systemDefaultZone() -->即相对于 ZoneId.systemDefault()默认时区
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//自定义时区
LocalDateTime now2 = LocalDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(now2);//会以相应的时区显示日期
//自定义时钟
Clock clock = Clock.system(ZoneId.of("Asia/Dhaka"));
LocalDateTime now3 = LocalDateTime.now(clock);
System.out.println(now3);//会以相应的时区显示日期
//不需要写什么相对时间 如java.util.Date 年是相对于1900 月是从0开始
//2013-12-31 23:59
LocalDateTime d1 = LocalDateTime.of(2013, 12, 31, 23, 59);
//年月日 时分秒 纳秒
LocalDateTime d2 = LocalDateTime.of(2013, 12, 31, 23, 59, 59, 11);
//使用瞬时时间 + 时区
Instant instant = Instant.now();
LocalDateTime d3 = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
System.out.println(d3);
//解析String--->LocalDateTime
LocalDateTime d4 = LocalDateTime.parse("2013-12-31T23:59");
System.out.println(d4);
LocalDateTime d5 = LocalDateTime.parse("2013-12-31T23:59:59.999");//999毫秒 等价于999000000纳秒
System.out.println(d5);
//使用DateTimeFormatter API 解析 和 格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime d6 = LocalDateTime.parse("2013/12/31 23:59:59", formatter);
System.out.println(formatter.format(d6));
//时间获取
System.out.println(d6.getYear());
System.out.println(d6.getMonth());
System.out.println(d6.getDayOfYear());
System.out.println(d6.getDayOfMonth());
System.out.println(d6.getDayOfWeek());
System.out.println(d6.getHour());
System.out.println(d6.getMinute());
System.out.println(d6.getSecond());
System.out.println(d6.getNano());
//时间增减
LocalDateTime d7 = d6.minusDays(1);
LocalDateTime d8 = d7.plus(1, IsoFields.QUARTER_YEARS);
//LocalDate 即年月日 无时分秒
//LocalTime即时分秒 无年月日
//API和LocalDateTime类似就不演示了
}
public static void testZonedDateTime() {
//即带有时区的date-time 存储纳秒、时区和时差(避免与本地date-time歧义)。
//API和LocalDateTime类似,只是多了时差(如2013-12-20T10:35:50.711+08:00[Asia/Shanghai])
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(now2);
//其他的用法也是类似的 就不介绍了
ZonedDateTime z1 = ZonedDateTime.parse("2013-12-31T23:59:59Z[Europe/Paris]");
System.out.println(z1);
}
public static void testDuration() {
//表示两个瞬时时间的时间段
Duration d1 = Duration.between(Instant.ofEpochMilli(System.currentTimeMillis() - 12323123), Instant.now());
//得到相应的时差
System.out.println(d1.toDays());
System.out.println(d1.toHours());
System.out.println(d1.toMinutes());
System.out.println(d1.toMillis());
System.out.println(d1.toNanos());
//1天时差 类似的还有如ofHours()
Duration d2 = Duration.ofDays(1);
System.out.println(d2.toDays());
}
public static void testChronology() {
//提供对java.util.Calendar的替换,提供对年历系统的支持
Chronology c = HijrahChronology.INSTANCE;
ChronoLocalDateTime d = c.localDateTime(LocalDateTime.now());
System.out.println(d);
}
/**
* 新旧日期转换
*/
public static void testNewOldDateConversion(){
Instant instant=new Date().toInstant();
Date date=Date.from(instant);
System.out.println(instant);
System.out.println(date);
}
public static void main(String[] args) throws InterruptedException {
testClock();
testInstant();
testLocalDateTime();
testZonedDateTime();
testDuration();
testChronology();
testNewOldDateConversion();
}
}

参考链接:

http://www.importnew.com/11908.html#methodReferences

https://my.oschina.net/benhaile/blog/193956

图片来源:八恶人(movie)

Java 8新特性之Date/Time(八恶人-4)的更多相关文章

  1. Java 8新特性之 Nashorn(八恶人-6)

    Joe Gage 盖奇·乔 “First time in my life I made a pretty penny.And, figured I'd come home and spend time ...

  2. Java 8新特性之Stream(八恶人-3)

    “You John Ruth The Hangman” 绞刑者鲁斯·约翰 “When the Hangman catches you, you hang.”当被绞刑者抓住了,你肯定会被绞死 一.基本介 ...

  3. Java 8新特性之lambda(八恶人-2)

    Major Marquis Warren 沃伦·马奎斯少校 “Tring to get a couple of bounties in to Red Rock.”我想带几个通缉犯去红石镇 一.基本介绍 ...

  4. Java 8新特性之 Optional(八恶人-5)

    Oswaldo Mobray 莫博瑞·奥斯瓦尔多 “I‘m Oswaldo Mobray, the hangman in these parts.” “我是莫博瑞·奥斯瓦尔多,这片地区的绞刑官.” 一 ...

  5. Java 8新特性之 Base64(八恶人-7)

    "General" 我是个将军 “ You, sir a hyena. I hava no wish to speak to you.”  “你就是一个土狗,你不配跟我说话” 一. ...

  6. Java 8新特性探究(八)精简的JRE详解

    http://www.importnew.com/14926.html     首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源 - 导航条 - 首页 所有文章 资讯 ...

  7. [转帖]Java 8新特性探究(八)精简的JRE详解

    Java 8新特性探究(八)精简的JRE详解 https://my.oschina.net/benhaile/blog/211804 精简版的api   撸了今年阿里.网易和美团的面试,我有一个重要发 ...

  8. Spring 4支持的Java 8新特性一览

    有众多新特性和函数库的Java 8发布之后,Spring 4.x已经支持其中的大部分.有些Java 8的新特性对Spring无影响,可以直接使用,但另有些新特性需要Spring的支持.本文将带您浏览S ...

  9. Java 8 新特性终极版

    声明:本文翻译自Java 8 Features Tutorial – The ULTIMATE Guide,翻译过程中发现并发编程网已经有同学翻译过了:Java 8 特性 – 终极手册,我还是坚持自己 ...

随机推荐

  1. Android之基于小米天气的天气源库

    大概去年的这个时候,有跟大家分享简洁天气这个应用. 该应用一開始使用的是中国天气网的数据,可是,由于须要反复多次请求server获取信息才干满足我们的需求,因此.后来我偷偷的将天气源更换成" ...

  2. android学习---Gallery画廊视图

    Gallery与Spinner有共同父类:AbsPinner.说明Gallery与Spinner都是一个列表框. 它们之间的差别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是 ...

  3. 20155238 2016-2017-2《Java程序设计》课程总结

    每周作业链接汇总(按顺序) 预备作业1 预备作业2 预备作业3 第一周作业 第二周作业 第三周作业 第四周作业 第五周作业 第六周作业 第七周作业 第八周作业 第九周作业 第十周作业 自认为写得最好一 ...

  4. [Python]-pip-ReadTimeoutError: Read timed out 问题

    问题描述 就是在安装Python包的时候,由于时间太长引起的超时问题 问题解决 第一个办法是更改源地址:在 ~/.pip/ 下创建文件 pip.conf(如果还没有的话), 模版如下: [global ...

  5. Android开发——Fragment知识整理(二)

    0.  前言 Android开发中的Fragment的应用非常广泛,在Android开发--Fragment知识整理(一)中简单介绍了关于Fragment的生命周期,常用API,回退栈的应用等知识.这 ...

  6. 【php增删改查实例】第四节 -自己 DIY 一个数据库管理工具

    本节介绍如何自己DIY一个数据库管理工具,可以在页面输入sql 进行简单的增删改查操作. 首先,找到xampp的安装目录,打开htdocs: 新建一个php文件,名称为 mysqladmin.php ...

  7. Scala学习(二)练习

    Scala控制结构和函数&练习 1. 一个数字如果为正数,则它的signum为1:如果是负数,则signum为-1:如果为0,则signum为0:编写一个函数来计算这个值 简单逻辑判断: 测试 ...

  8. springboot项目生成jar包(带静态资源)方法

    [Maven]在pom.xml文件中使用resources插件的小作用 不过war包比较实用,毕竟独立的tomcat比较好控制

  9. 爬虫利器_you-get

    用Python做爬虫也很久了,今天分享一个轻巧的爬虫库:you-get you-get 是用 Python3写成的视频,图片,音频下载工具,堪称盗链,爬虫神器.其支持的网站,都是直接破解其算法,直接算 ...

  10. 通过Mysql连接ASP.Net Core2.0(Code First模式)

    ASP.NET Core2.0连接Mysql,首先新建项目 选择Web应用程序 选择需要身份验证: 通过Nuget安装Mysql驱动,这里推荐>Pomelo.EntityFrameworkCor ...