一、新的Date API介绍

LocalDate
LocalTime
Instant
Duration
Period

formatter
parsejdk以前的java.util.Date存在的问题

1)比如new Date(119, 2, 18)表示Mon Mar 18 00:00:00 CST 2019,2019年3月18日,year要从1900年加起,month是从0开始,day是从1开始。

2)SimpleDateFormat不是线程安全的,比如多线程情况下simpleDateFormat.parse会出问题。

3)Date名字叫日期,但是后面还有time时间

例子如下:

 package com.cy.java8;

 import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class DateTest { public static void main(String[] args) throws ParseException {
Date date = new Date(119, 2, 18);
System.out.println(date); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
for(int i=0; i<5; i++){
new Thread(()->{
try {
Date parseDate = sdf.parse("20190505");
System.out.println(parseDate);
} catch (ParseException e) {
e.printStackTrace();
} }).start();
}
}
}

二、LocalDate      

 package com.cy.java8;

 import java.time.LocalDate;
import java.time.temporal.ChronoField; public class DateTest { public static void main(String[] args) {
testLocalDate();
} /**
* LocalDate是线程安全的
*/
private static void testLocalDate(){
LocalDate localDate = LocalDate.of(2019, 10, 2);
System.out.println(localDate.getYear());
System.out.println(localDate.getMonth());
System.out.println(localDate.getMonthValue());
System.out.println(localDate.getDayOfYear());
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek()); System.out.println(localDate.get(ChronoField.YEAR));
System.out.println(localDate.get(ChronoField.MONTH_OF_YEAR));
System.out.println(localDate.get(ChronoField.DAY_OF_MONTH)); LocalDate now = LocalDate.now();
System.out.println(now);
}
}

console:

2019
OCTOBER
10
275
2
WEDNESDAY
2019
10
2
2019-10-02

三、LocalTime、Instant、Duration、Period简单介绍

 package com.cy.java8;

 import java.time.*;

 public class DateTest {

     public static void main(String[] args) throws InterruptedException {
testLocalTime();
System.out.println("================================="); testCombineLocalDateAndTime();
System.out.println("================================="); testInstant();
System.out.println("================================="); testDuration();
System.out.println("================================="); testPeriod();
} private static void testLocalTime(){
LocalTime time = LocalTime.now();
System.out.println(time); System.out.println(time.getHour());
System.out.println(time.getMinute());
System.out.println(time.getSecond());
} private static void testCombineLocalDateAndTime(){
LocalDate localDate = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.of(localDate, time);
System.out.println(localDateTime); LocalDateTime now = LocalDateTime.now();
System.out.println(now);
} private static void testInstant() throws InterruptedException {
Instant start = Instant.now();
Thread.sleep(1000);
Instant end = Instant.now(); Duration duration = Duration.between(start, end); //类似于之前的System.currentTimeMillis
System.out.println(duration.toMillis());
} private static void testDuration(){
LocalTime time = LocalTime.now();
LocalTime beforeTime = time.minusHours(1);
Duration duration = Duration.between(time, beforeTime);
System.out.println(duration.toHours());
} private static void testPeriod(){
Period period = Period.between(LocalDate.of(2017, 9, 1), LocalDate.of(2019, 10, 2));
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
}

console:

21:03:28.136
21
3
28
=================================
2019-10-02T21:03:28.137
2019-10-02T21:03:28.137
=================================
1001
=================================
-1
=================================
2
1
1

  

四、formatter、parse

 package com.cy.java8;

 import java.time.*;
import java.time.format.DateTimeFormatter; public class DateTest { public static void main(String[] args){
testDateFormat();
System.out.println("=============================="); testDateParse();
} private static void testDateFormat(){
LocalDate localDate = LocalDate.now();
String format1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(format1); String format2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(format2); DateTimeFormatter myFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String format3 = localDate.format(myFormatter);
System.out.println(format3);
} private static void testDateParse(){
String date0 = "20191002";
LocalDate localDate0 = LocalDate.parse(date0, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(localDate0); String date1 = "2019-10-02";
LocalDate localDate1 = LocalDate.parse(date1, DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(localDate1); String date2 = "2019年10月02日";
LocalDate localDate2 = LocalDate.parse(date2, DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
System.out.println(localDate2);
}
}

console:

20191002
2019-10-02
2019年10月02日
==============================
2019-10-02
2019-10-02
2019-10-02

----

New Date API介绍的更多相关文章

  1. Servlet基础(一) Servlet简介 关键API介绍及结合源码讲解

    Servlet基础(一) Servlet基础和关键的API介绍 Servlet简介 Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中. Servlet容器负责Servl ...

  2. Commons-lang API介绍

    4.1 Commons-lang API介绍 4.1.1 StringUtils 4.1.2 StringEscapeUtils 4.1.3 ArrayUtils 4.1.4 DateUtils 4. ...

  3. 乐字节-Java8新特性之Date API

    上一篇文章,小乐给大家带来了Java8新特性之Optional,接下来本文将会给大家介绍Java8新特性之Date API 前言: Java 8通过发布新的Date-Time API来进一步加强对日期 ...

  4. Solr系列三:solr索引详解(Schema介绍、字段定义详解、Schema API 介绍)

    一.Schema介绍 1. Schema 是什么? Schema:模式,是集合/内核中字段的定义,让solr知道集合/内核包含哪些字段.字段的数据类型.字段该索引存储. 2. Schema 的定义方式 ...

  5. 常用ArcGIS for Silverlight 开发API介绍

    1.API介绍 2.Map对象  3.Layer对象 4.Symbol对象 5.Task对象

  6. python学习笔记(win32print API介绍)

    最近博主在研究用python控制打印机 这里整理下win32print的API介绍,官网地址http://timgolden.me.uk/pywin32-docs/win32print.html Op ...

  7. js 获取时间 new Date()详细介绍

    javaScript系列:js中获取时间new Date()详细介绍 (2012-03-31 09:54:25) 转载▼ 标签: js时间 new date() 字符类型 转换 分类: study-j ...

  8. 使用html5中video自定义播放器必备知识点总结以及JS全屏API介绍

    一.video的js知识点: controls(控制器).autoplay(自动播放).loop(循环)==video默认的: 自定义播放器中一些JS中提供的方法和属性的记录: 1.play()控制视 ...

  9. JavaScript Date对象介绍

    原文:JavaScript Date对象介绍 Date 日期和时间对象 1. 介绍 Date对象,是操作日期和时间的对象.Date对象对日期和时间的操作只能通过方法. 2. 构造函数 2.1 new ...

随机推荐

  1. 六,k8s集群service资源

    目录 Service简介 ClusterIP Headless(无头service) NodePort Service简介 service的基本说明: Service 是作用于客户端可服务端(Pod) ...

  2. SSH中直接运行php文件

    cd /home/afish/domains/afish.cnblogs.com/public_htmlphp locoy_im_folder.php php locoy_im.php

  3. java7连接数据库 网页 添加学生信息测试

    石家庄铁道大学2019年秋季   2018 级课堂测试试卷(六)(10分) 课程名称: JAVA语言程序设计  任课教师: 王建民        考试时间: 150 分钟 一. 考试要求: 1登录账号 ...

  4. vue cli3.0快速搭建项目详解(强烈推荐)

    这篇文章主要介绍下vue-cli3.0项目搭建,项目结构和配置等整理一下,分享给大家. 一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cl ...

  5. nginx第二天

    nginx配置文件 配置文件结构 全局配置(user.worker_processes.error_log.pid) events(网络连接相关,worker_connections) http(最重 ...

  6. 各种环境下搭建ruby on rails开发环境

    win10上搭建raby on rails环境: 步骤如下 1.安装ruby (我选择的版本是ruby 2.2.3p173) 2.安装rails gem 在这之前建议先把gem的源换成淘宝的源,速度快 ...

  7. mysql之单表条件查询

    create table staff_info( id int primary key auto_increment, name varchar(32) not null, age int(3) un ...

  8. JAVA笔记7-Object类之toString方法和equals方法

    位于rt.jar\java\lang\Object.class Object类是所有Java类的根基类(Java中只有一个根基类,C++中有多个).如果在类声明中未使用extends关键字指明其基类, ...

  9. BZOJ 3589: 动态树 树链剖分+线段树+树链的并

    利用树剖序的一些性质~ 这个题可以出到 $\sum k=10^5$ 左右. 做法很简单:每次暴力跳重链,并在线段树上查询链和. 查询之后打一个标记,把加过的链都置为 $0$.这样的话在同一次询问时即使 ...

  10. Flyway Validate failed: Migration checksum mismatch for migration version 1.0.0.01 错误

    在运行系统的时候出现错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ...