代码:

package com.ufo.java8datetime;

import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; public class Java8DateTime {
public static void main(String[] args) {
// 取今天日期
LocalDate today=LocalDate.now();
System.out.println("Today's date="+today);
System.out.println("Today's year="+today.getYear());
System.out.println("Today's month="+today.getMonthValue());
System.out.println("Today's day="+today.getDayOfMonth()); // 特定日期
LocalDate devoiceDay=LocalDate.of(2020, 1, 16);
System.out.println("Devoice date="+devoiceDay); // 日期相等比较
if(today.equals(devoiceDay)==false) {
System.out.println("today !=devoiceDay ");
} // 一周后
LocalDate aWeekLater=today.plus(1, ChronoUnit.WEEKS);
System.out.println("A week later="+aWeekLater); // 一年前
LocalDate aYearBefore=today.plus(-1, ChronoUnit.YEARS);
System.out.println("A Year before="+aYearBefore); // 日期比较
LocalDate longAgo=LocalDate.of(2000, 1, 1);
if(longAgo.isBefore(aYearBefore)) {
System.out.println(longAgo+" is before "+aYearBefore);
} // 日期比较
if(today.isAfter(aYearBefore)) {
System.out.println(today+" is after "+aYearBefore);
} // 年份差
Period years=Period.between(longAgo, aYearBefore);
System.out.println("There are "+years.getYears()+" years between "+longAgo+" "+aYearBefore); // 当前时间
LocalTime currTime=LocalTime.now();
System.out.println("Current time="+currTime); // 时间加
LocalTime threehoursLater=currTime.plusHours(3);
System.out.println("3 hours later="+threehoursLater); // 取毫秒时间戳
Clock clock=Clock.systemUTC();
System.out.println(clock.millis()+" == "+System.currentTimeMillis()); // 日期转字符串
DateTimeFormatter format1=DateTimeFormatter.ofPattern("yyyy年MM月dd日");
System.out.println(today.format(format1)); // 字符串转日期
LocalDate date1=LocalDate.parse("1644年03月14日", format1);
System.out.println(date1);
}
}

输出:

Today's date=2020-01-20
Today's year=2020
Today's month=1
Today's day=20
Devoice date=2020-01-16
today !=devoiceDay
A week later=2020-01-27
A Year before=2019-01-20
2000-01-01 is before 2019-01-20
2020-01-20 is after 2019-01-20
There are 19 years between 2000-01-01 2019-01-20
Current time=20:40:57.528
3 hours later=23:40:57.528
1579524057528 == 1579524057528
2020年01月20日
1644-03-14

2020年1月20日

Java8的日期时间处理的更多相关文章

  1. 基于Java8的日期时间工具类DateTimeFormatter

    原文:https://blog.csdn.net/qq_36596145/article/details/85331002 import java.time.Instant; import java. ...

  2. 详解Java8的日期和时间API

    详解Java8的日期和时间API 在JDK1.0的时候,Java引入了java.util.Date来处理日期和时间:在JDK1.1的时候又引入了功能更强大的java.util.Calendar,但是C ...

  3. java8 中的时间和数据的变化

    java8除了lambda表达式之外还对时间和数组这两块常用API做想应调整, Stream 有几个常用函数: store 排序 (a,b)-> a.compareTo(b)  排出来的结果是正 ...

  4. Java8 日期/时间(Date Time)使用简介

    特别说明: LocalDateTime 为日期时间的计算提供了很大的方便, 在构造对象/运算/toString等方便都非常便利. 3个常用的类: java.time.LocalDateTime; ja ...

  5. java8新的时间日期库及使用示例

    转自:https://www.cnblogs.com/comeboo/p/5378922.html 来自:Java译站 链接:http://it.deepinmind.com/java/2015/03 ...

  6. Java8学习笔记(九)--日期/时间(Date Time)API指南

    Java 8日期/时间( Date/Time)API是开发人员最受追捧的变化之一,Java从一开始就没有对日期时间处理的一致性方法,因此日期/时间API也是除Java核心API以外另一项倍受欢迎的内容 ...

  7. Java8获取当前时间、新的时间日期类如Java8的LocalDate与Date相互转换、ZonedDateTime等常用操作包含多个使用示例、Java8时区ZoneId的使用方法、Java8时间字符串解析成类

     下面将依次介绍 Date转Java8时间类操作 ,Java8时间类LocalDate常用操作(如获得当前日期,两个日期相差多少天,下个星期的日期,下个月第一天等) 解析不同时间字符串成对应的Java ...

  8. Java8 中的时间和日期 API

    1. 日期和时间概述 LocalDate,LocalTime,LocalDateTime类的实例是不可变的对象,分别表示使用 ISO-8601 日历系统 的日期,时间,日期和时间;它们提供了简单的日期 ...

  9. [转] Java8 日期/时间(Date Time)API指南

    [From] http://www.importnew.com/14140.html Java 8日期/时间( Date/Time)API是开发人员最受追捧的变化之一,Java从一开始就没有对日期时间 ...

随机推荐

  1. 【工具】之002-Mac下常用工具

    写在前面 我很懒,,,不想敲一个命令一个命令敲... "偷懒是有前提的,不是之前,就是之后." 常用命令 测试端口是否畅通 nc -z 10.254.3.86 30003 查看端口 ...

  2. 程序员必须了解!IntelliJ IDEA 2020.2的新增功能

    IDEA 因为之前破解过,所以家里的电脑都是19版本的,用的也比较顺手,也就一直懒得去动他,但是,程序猿的好奇心可能真的挺重,猎奇心里,在网上也看到了很多关于2020版本的idea的各种好处,于是,闲 ...

  3. C#设计模式之0-简单工厂模式

    简单工厂模式(Simple Factory Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/387 访问 ...

  4. MongoDB学习4:MongoDB复制集机制和原理,搭建复制集

    1.复制集的作用   1.1 MongoDB复制集的主要意义在于实现服务高可用   1.2 它的实现依赖于两个方面的功能:    · 数据写入时将数据迅速复制到另一个独立节点上    · 在接收写入的 ...

  5. 听说同学你搞不懂Java的LinkedHashMap,可笑

    先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个有颜值却假装靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有我精心为你准备的一线大厂面试题 ...

  6. 报错:Cause: java.sql.SQLSyntaxErrorException: ORA-00936: 缺失表达式

    错误原因:

  7. ubuntu开发机所需工具,做个记录,不断补充

    文件搜索 FSearch 用了下可以, 类似windows下的Everything 或者mac的cmd+空格 地址 安装: sudo add-apt-repository ppa:christian- ...

  8. 【CF1174D】 Ehab and the Expected XOR Problem - 构造

    题面 Given two integers \(n\) and \(x\), construct an array that satisfies the following conditions: · ...

  9. Android TextView 字数过多,用跑马灯滚动形式实现

    上代码: <TextView android:layout_width="120dp" android:layout_height="wrap_content&qu ...

  10. 微信DLL劫持反弹shell复现

    (该文参考网络他人资料,仅为学习,不许用于非法用途) 一.操作环境 Windows7 :  微信  , Process Explorer(任务管理工具,本实验中用于查找微信程序调用的DLL文件) Ka ...