New Date API介绍
一、新的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介绍的更多相关文章
- Servlet基础(一) Servlet简介 关键API介绍及结合源码讲解
Servlet基础(一) Servlet基础和关键的API介绍 Servlet简介 Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中. Servlet容器负责Servl ...
- 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. ...
- 乐字节-Java8新特性之Date API
上一篇文章,小乐给大家带来了Java8新特性之Optional,接下来本文将会给大家介绍Java8新特性之Date API 前言: Java 8通过发布新的Date-Time API来进一步加强对日期 ...
- Solr系列三:solr索引详解(Schema介绍、字段定义详解、Schema API 介绍)
一.Schema介绍 1. Schema 是什么? Schema:模式,是集合/内核中字段的定义,让solr知道集合/内核包含哪些字段.字段的数据类型.字段该索引存储. 2. Schema 的定义方式 ...
- 常用ArcGIS for Silverlight 开发API介绍
1.API介绍 2.Map对象 3.Layer对象 4.Symbol对象 5.Task对象
- python学习笔记(win32print API介绍)
最近博主在研究用python控制打印机 这里整理下win32print的API介绍,官网地址http://timgolden.me.uk/pywin32-docs/win32print.html Op ...
- js 获取时间 new Date()详细介绍
javaScript系列:js中获取时间new Date()详细介绍 (2012-03-31 09:54:25) 转载▼ 标签: js时间 new date() 字符类型 转换 分类: study-j ...
- 使用html5中video自定义播放器必备知识点总结以及JS全屏API介绍
一.video的js知识点: controls(控制器).autoplay(自动播放).loop(循环)==video默认的: 自定义播放器中一些JS中提供的方法和属性的记录: 1.play()控制视 ...
- JavaScript Date对象介绍
原文:JavaScript Date对象介绍 Date 日期和时间对象 1. 介绍 Date对象,是操作日期和时间的对象.Date对象对日期和时间的操作只能通过方法. 2. 构造函数 2.1 new ...
随机推荐
- 爬虫之如何找js入口(一)
目标网页:https://m.gojoy.cn/pages/login/ 将我删除i ndex?from=%2Fpages%2Fuser%2Findex 需要工具:chrome和油猴 油猴代码: // ...
- LeetCode_Bit Manipulation
231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...
- 动画学习之WIFI图形绘制
Android原生动画概述: 对于APP开发中涉及到的一些动画基本上都可以用Android提供的各种原生动画类来实现,所以在学习自定义动画之前首先来对原生动画进行一个基本的了解,这里不详细对每一个原生 ...
- HashMap,LinkedHashMap,TreeMap的有序性
HashMap 实际上是一个链表的数组.HashMap 的一个功能缺点是它的无序性,被存入到 HashMap 中的元素,在遍历 HashMap 时,其输出是无序的.如果希望元素保持输入的顺序,可以使用 ...
- 遗传算法解决寻路问题——Python描述
概要 我的上一篇写遗传算法解决排序问题,当中思想借鉴了遗传算法解决TSP问题,本质上可以认为这是一类问题,就是这样认为:寻找到一个序列X,使F(X)最大. 详解介绍 排序问题:寻找一个序列,使得这个序 ...
- noi.ac NA529 【神树的矩阵】
表示今天一发A了这题拿了rk3...是个sb构造... 考虑除了\(n=1/m=1\)的情况,最小次数\(ans\)不会\(>3\). 对于\(n=1/m=1\),暴力即可. 然后考虑\(ans ...
- Kendo UI for jQuery使用教程:小部件DOM元素结构
[Kendo UI for jQuery最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support ...
- 关于distinct的compare参数的使用
1.创建compare类 using DCZY.Bean; using System; using System.Collections.Generic; using System.Linq; usi ...
- xpath的编写规则
xpath的编写规则是// 表示从任意一级开始,或间隔任意级.换句话说中间就是可以隔很多层/ 从根目录开始,或从上一层的次层开始,就是需要跟上一层是上下级关系@id=aaa,id=aaa的元素,和元素 ...
- Mybaits 查询 choose when 的使用
@Select("<script>"+ "SELECT * " + "FROM bgs_housing A" + " ...