Java 8 – Date Time API

Java 8 comes with a much improved and much required change in the way date and time is handled.
The classes that represent the date and time concepts are present in the java.time package. The important classes in this package are:
  • java.time.Period: This class represents the date part of the datetime. It represents the date part in terms of days, months and years. for example 1 year, 2 months and 5 days
  • java.time.Duration: This class represents the time part of the datetime. It represents the time part in terms of seconds and nanoseconds. for example ’29 seconds’
  • java.time.Instant: This represents a particular instant in the timeline. It stores the number of seconds through epoch time and also has another field that stores the nanoseconds
  • java.time.LocalDate: This stores the date part of date-time in terms of years-months-days. It does not store the TimeZone. The class is immutable.
  • java.time.LocalTime: This class stores the time part of date time without any TimeZone info.
  • java.time.LocalDateTime: This class stores the LocalDate and LocalTime but no TimeZone
  • java.time.ZonedDateTime: This class stores the LocalDateTime and the TimeZone info as a ZoneOffset object. The class has access to ZoneRules which can be used to convert to local time
  • java.time.ZoneOffset:This stores the time zone offset from UTC. The zone rules are stored in ZoneId.
  • java.time.OffsetDateTime:This stores the local datetime with the offset. This class does not have information about Zone Rules.
 package java8.datetime;

 import java.time.LocalDate;
import java.time.LocalTime; /**
* Java 8 – Date Time API
* LocalTime:当前时间
* LocalDate:当前日期
*/
public class LocalTimeDemo3 {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
System.out.println("当前时间:" + localTime); LocalDate localDate = LocalDate.now();
System.out.println("当前日期:" + localDate);
}
}
LocalDateTime
 package java8.datetime;

 import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoField; /**
* Java 8 – Date Time API
* LocalDateTime
* url:http://www.studytrails.com/java/java8/java8_date_and_time/
*/
public class LocalDateTimeDemo2 {
public static void main(String[] args) {
// create a LocalDateTime
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("新建LocalDateTime对象:" + localDateTime); // prints 2018-02-12T15:08:44.116 // Convert LocalDateTime to datetime in particular zone
System.out.println("指定时区:" + localDateTime.atZone(ZoneId.of("America/New_York"))); // Get the day of week from DateTime
System.out.println("星期几:" + DayOfWeek.from(localDateTime)); // prints SUNDAY. // Get the day of year from DateTime
System.out.println("当年第几天:" + localDateTime.get(ChronoField.DAY_OF_YEAR)); // prints 43
// The other fields that can be returned are MINUTE_OF_HOUR, MINUTE_OF_DAY, HOUR_OF_AMPM,
// HOUR_OF_DAY, AMPM_OF_DAY, DAY_OF_WEEK, DAY_OF_MONTH, DAY_OF_YEAR, MONTH_OF_YEAR,
// YEAR, OFFSET_SECONDS (offset from UTC). // 获得当前日期
System.out.println("当前日期:" + localDateTime.toLocalDate());
// prints 2014-09-29 // 获取当前时间
System.out.println("当前时间:" + localDateTime.toLocalTime());
// prints 22:26:30.146 // Create a LocalDateTime from year, month, day, hour, min
System.out.println("实例化LocalDateTime by Int:" + LocalDateTime.of(2014, 10, 1, 10, 0));
// prints 2014-10-01T10:00 // Create LocalDateTime by parsing a string
LocalDateTime parsedLocalDateTime = LocalDateTime.parse("2014-01-01T11:00");
System.out.println("实例化LocalDateTime by String:" + parsedLocalDateTime); System.out.println("当前小时点:" + localDateTime.getHour()); }
}

Instant

 package java8.datetime;

 import java.time.Instant;
import java.time.temporal.ChronoUnit; /**
* Java 8 – Date Time API
* Instant
* url:http://www.studytrails.com/java/java8/java8_date_and_time/
*/
public class InstantDemo1 {
public static void main(String[] args) {
//Creating a local date
Instant now = Instant.now();
//2018-02-12T06:29:01.493Z
System.out.println("当前时间:" + now); // The epoch seconds is the number of seconds since 1970-01-01T00:00:00Z
System.out.println("当前秒数:" + now.getEpochSecond()); // 调整时间
// the plus function allows adding time intervals.
// The time intervals can be NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS, DAYS
Instant tomorrow = now.plus(2, ChronoUnit.DAYS);
System.out.println("明天时间:" + tomorrow);
// The minus function allows subtracting time from an instant.
Instant yesterday = now.minus(1, ChronoUnit.DAYS);
System.out.println("昨天时间:" + yesterday); // The compare function can be used to compare two dates.
// It returns -1 if the date that is passed is after , 1 if it is before
System.out.println("今天和明天比较:" + now.compareTo(tomorrow));// prints -1 // check if one instant is after another
System.out.println("今天是否在昨天后面:" + now.isAfter(yesterday));// prints true }
}

Java之Date Time API (Java 8 新特性)的更多相关文章

  1. Java 8 Date Time API Example Tutorial – LocalDate, Instant, LocalDateTime, Parse and Format

    参考 Java 8 Date and Time API is one of the most sought after change for developers. Java has been mis ...

  2. Java知识回顾 (18)Java 8、9、11的新特性

    Java 8 Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本. Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ,它支持函数式编程,新的 Jav ...

  3. Java单元測试工具JUnit 5新特性一览

    Java单元測试工具JUnit 5新特性一览 作者:chszs,未经博主同意不得转载. 经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs JUnit是最流行的开源 ...

  4. java 与日期转换相关的方法(java.util.date类型和java.sql.date类型互相转换)、随机字符串生成方法、UUID生产随机字符串

    package com.oop.util; import java.text.*; import java.util.UUID; import org.junit.Test; /* * 与日期相关的工 ...

  5. ASP.NET Web API 2.0新特性:Attribute Routing1

    ASP.NET Web API 2.0新特性:Attribute Routing[上篇] 对于一个针对ASP.NET Web API的调用请求来说,请求的URL和对应的HTTP方法的组合最终决定了目标 ...

  6. 尝新体验ASP.NET Core 6预览版本中发布的最小Web API(minimal APIS)新特性

    本文首发于<尝新体验ASP.NET Core 6预览版本中发布的最小Web API(minimal APIS)新特性> 概述 .NET开发者们大家好,我是Rector. 几天前(美国时间2 ...

  7. 【java】JDK1.8时间日期库 新特性 所有java中时间Date的使用

    除了lambda表达式,stream以及几个小的改进之外,Java 8还引入了一套全新的时间日期API,在本篇教程中我们将通过几个简单的任务示例来学习如何使用java 8的这套API.Java对日期, ...

  8. Java Development Kit (JDK) 发展历程 及新特性

    SE(J2SE),standard edition,标准版,是我们通常用的一个版本,从JDK 5.0开始,改名为Java SE. EE(J2EE),enterprise edition,企业版,使用这 ...

  9. 一次电话Java面试的问题总结(JDK8新特性、哈希冲突、HashMap原理、线程安全、Linux查询命令、Hadoop节点)

    面试涉及问题含有: Java JDK8新特性 集合(哈希冲突.HashMap的原理.自动排序的集合TreeSet) 多线程安全问题 String和StringBuffer JVM 原理.运行流程.内部 ...

随机推荐

  1. 扫盲记-第六篇--Normalization

    深度学习模型中的Normalization 数据经过归一化和标准化后可以加快梯度下降的求解速度,这就是Batch Normalization等技术非常流行的原因,Batch Normalization ...

  2. Echo团队Alpha冲刺随笔 - 第三天

    项目冲刺情况 进展 完成了三分一左右,前端整体页面框架已有,后端也在稳步推进 问题 今天问题较少,主要还是出在对于框架的掌握上 心得 继续加油! 今日会议内容 黄少勇 今日进展 实现社区公告,个人信息 ...

  3. 18-(基础入门篇)GPRS(Air202)拨打电话--(由于板子做修订,所以暂停更新)

    https://www.cnblogs.com/yangfengwu/p/9968883.html 这个直接用官方给的demo就可以 先睹为快 现在说个需求哈,是当初一个人给提出的需求 例如存入的号码 ...

  4. keystone系列一:keystone基础

    一 什么是keystone keystone是OpenStack的身份服务,暂且可以理解为一个'与权限有关'的组件. 二 为何要有keystone Keystone项目的主要目的是为访问opensta ...

  5. Python写代码的时候为什么要注释?Sun因此被Oracle收购

    导读: 此块分为:1.注释的重要性 2.如何正确注释 注释的重要性 在我们看代码的时候,会遇到很多看不懂得代码,特别是在做项目的时候,代码的注释以及命名习惯的重要性就有了为什么这么说呢? 因为在很多情 ...

  6. redis 配置 架构 基础

    redis 官网  redis.io   io为某国家域名后缀 有redis各种版本. java 版本 又分各种工具 clients 下 RedisClient为图形化管理界面 Jedis 才是jav ...

  7. [Oracle]TM lock (DML enqueue) 的相容性

    [Oracle]TM lock (DML enqueue) 的相容性 RS(SS):  行共享     LMODE =2 RX(SX):  行独占     LMODE =3 S:       共享   ...

  8. C# 爬虫 正则、NSoup、HtmlAgilityPack、Jumony四种方式抓取小说

    心血来潮,想爬点小说.通过百度选择了个小说网站,随便找了一本小说http://www.23us.so/files/article/html/13/13655/index.html. 1.分析html规 ...

  9. Webpack 2 视频教程 005 - Webpack 编译输出日志

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  10. Nginx 负载均衡的Cache缓存批量清理的操作记录

    1)nginx.conf配置 [root@inner-lb01 ~]# cat /data/nginx/conf/nginx.conf user www; worker_processes 8; #e ...