一、新的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. C#异步编程研究学习(一)

    可以使用Func<T>或者Action<T>简单实现如: Func<string, string,string,string, int> func = new Fu ...

  2. jmeter解析response里的json对象和数组

    1.解析提取json对象 2.解析提取json数组 注意,标红这里是从0开始计数 提取最后一个数组

  3. 使用CreateMetaFile创建WMF文件,并转换为EMF文件

    #include <iostream> #include <stdio.h> #include <WINDOWS.H> #include <shellapi. ...

  4. 第十一章 前端开发-html

    第十一章 前端开发-html 1.1.0 html:超文本标记语言 html特征:(HyperText Markup Language) 对换行的空格不敏感 空白折叠 标签:有称为标记 双闭合标签 & ...

  5. 2017 网易游戏互娱游戏研发4.21(offer)

    网易游戏互娱(offer) 去年这个时候就参加过网易游戏的实习生招聘,到今年总共收到了4次拒信.不过这次运气好,终于get了最想要的offer.去年实习生互娱笔试挂,秋招笔试挂,今年春招互娱投了连笔试 ...

  6. buuctf@easyre

  7. setAttribute()方法和 getAttribute() 方法

    一.setAttribute() 方法 setAttribute() 方法为一个或一组元素添加指定的属性,并且为其赋指定的值.(主要针对自定义属性) 如果这个属性已经存在,仅仅设置或是修改属性值. 浏 ...

  8. CSS3 3D转换——rotateX(),rotateY(),rotateZ()

    CSS3 允许使用 3D 转换来对元素进行格式化. ㈠浏览器支持 Internet Explorer 10 和 Firefox 支持 3D 转换. Chrome 和 Safari 需要前缀 -webk ...

  9. jquery isDefaultPrevented()方法 语法

    jquery isDefaultPrevented()方法 语法 作用:isDefaultPrevented() 方法返回指定的 event 对象上是否调用了 preventDefault() 方法. ...

  10. TTTTTTTTTTTT 百度之星D map+hash

    Problem D  Accepts: 2806  Submissions: 8458  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6 ...