Java8时间的简单时间
package com.java8.date; import org.junit.Test; import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.*; public class DateTest { @Test
public void LocalDateTest() { // of方法获取一个指定日期的LocalDate
LocalDate date1 = LocalDate.of(2018, 12, 29);
System.out.println(date1);
System.out.println(date1.getYear());
System.out.println(date1.getMonth()); System.out.println(date1.getMonthValue());
System.out.println(date1.getDayOfMonth());
System.out.println(date1.getDayOfWeek());
System.out.println(date1.getDayOfWeek().getValue());
System.out.println(date1.getDayOfYear()); System.out.println("判断时间前后关系:" + date1.isBefore(date1)); // now 获取当前时间
System.out.println(LocalDate.now()); // 获取指定字段的值
System.out.println(date1.get(ChronoField.YEAR));
System.out.println(date1.get(ChronoField.MONTH_OF_YEAR));
System.out.println(date1.get(ChronoField.DAY_OF_YEAR));
System.out.println(date1.get(ChronoField.DAY_OF_MONTH)); // 多了一些加减运算
//Peroid是针对日期的 , Duration 主要是针对Time的
System.out.println(date1.minus(Period.ofYears(2)));
System.out.println(date1.minus(Period.ofDays(2)));
System.out.println(date1.minus(Period.ofWeeks(2))); Period between = Period.between(date1.minus(Period.ofYears(2)), date1);
Period between2 = Period.between(date1.minus(Period.ofMonths(2)), date1); System.out.println(between.getMonths());
System.out.println(between2.getMonths());
System.out.println("date1.minus(between) = " + date1.minus(between)); LocalDate now = LocalDate.now();
// 替换年
System.out.println(now.withYear(2016)); // 计算某一个时间字段的取值范围
System.out.println(now.range(ChronoField.DAY_OF_MONTH));
} @Test
public void LocalTimeTest() { LocalTime now = LocalTime.now();
System.out.println(now); LocalTime time = LocalTime.of(12, 12);
System.out.println(time); System.out.println(time.isBefore(now));
// 由于是time所以不支持年字段
System.out.println(time.isSupported(ChronoField.YEAR));
System.out.println(time.isSupported(ChronoUnit.YEARS));
System.out.println(time.isSupported(ChronoUnit.HOURS)); System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss"))); // Duration 主要是针对Time的,Peroid是针对日期的
System.out.println(" time.minus(Duration.ofHours(2)) = " + time.minus(Duration.ofHours(2)));
System.out.println(" time.plus(Duration.ofHours(2)) = " + time.plus(Duration.ofHours(2))); } @Test
public void LocalDateTimeTest() { LocalDateTime dateTime = LocalDateTime.now();
//2018-12-29T13:38:03.212
System.out.println(dateTime.toString()); dateTime = LocalDateTime.of(2018, 12, 29, 12, 12); System.out.println(dateTime); //指定今天的12点12分 LocalDateTime toDay1212 = LocalDate.now().atTime(LocalTime.of(12, 12));
System.out.println(toDay1212); //DateTime转Date
System.out.println(toDay1212.toLocalDate());
//DateTime转Time
System.out.println(toDay1212.toLocalTime());
// 替换某一个时间单位
System.out.println(toDay1212.withHour(14)); } @Test
public void instantTest() { System.out.println(Instant.now());
System.out.println(System.currentTimeMillis());
System.out.println(System.nanoTime());
System.out.println(Instant.now().toEpochMilli());
System.out.println(Instant.now().plusNanos(0));
System.out.println(Instant.now().get(ChronoField.NANO_OF_SECOND)); Period between = Period.between(LocalDate.of(2018, 12, 26), LocalDate.of(2018, 12, 28)); System.out.println(between.getDays());
// between表示是一个间隔,做加减法时候只拿间隔做运算,不考虑具体的起止日期
System.out.println(LocalDateTime.now().plus(between)); } @Test
public void TemporalAdjusterTest() { // 计算当前日期的加一个周五
System.out.println(LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
// 计算该月的最后一天
System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()));
// 计算该年的最后一天
System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfYear())); } @Test
public void DateTimeFormatterTest() {
//DateTimeFormatter 线程安全线的 ,SimpleDateFormat线程不安全原因是底层公用一个Calender成员
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); } @Test
public void ZoneIdTest() { System.out.println(ZoneId.systemDefault()); }
}
Java8时间的简单时间的更多相关文章
- 详解Java8的日期和时间API
详解Java8的日期和时间API 在JDK1.0的时候,Java引入了java.util.Date来处理日期和时间:在JDK1.1的时候又引入了功能更强大的java.util.Calendar,但是C ...
- Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法:
Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法: ------------------------------------------------------------ ...
- Python3.x:简单时间调度Timer(间隔时间执行)
Python3.x:简单时间调度Timer(间隔时间执行) threading模块中的Timer能够帮助实现定时任务,而且是非阻塞的: 代码: import threading import time ...
- 减少Qt编译时间暨简单Qt裁剪
本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作.原文链接:减少Qt编译时间暨简单Qt裁剪 编译一 ...
- 都9012了,Java8中的日期时间API你还没有掌握?
一,Java8日期时间API产生的前因后果 1.1 为什么要重新定义一套日期时间API 操作不方便:java中最初的Date不能直接对指定字段进行加减操作也不支持国际化,后来新增了Calendar,但 ...
- linux 时间处理 + 简单写log
1s ==1000ms == 1,000,000us == 1,000,000,000 nanosecond uname -a Linux scott-Z170X 4.15.0-34-generic ...
- Linux系统时间与RTC时间【转】
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3637782 Linux的RTC驱动相对还是比较简单的,可以将它作为一个普通的字符 ...
- ftp上来显示的时间和系统时间不一致
ftp上来显示的时间和系统时间不一致,是因为默认情况下,vsftpd 是用GMT做为他的时间的,所以和系统的时间可能会不一致 修改也非常简单: vi /etc/vsftpd/vsftpd.conf 在 ...
- Laravel / Lumen 框架修改 创建时间 和 更新时间 对应字段
为避免浪费时间--先上解决方案 在Model中重写 CREATED_AT 和 UPDATED_AT 两个类常量就可以了,这两个常量分别是创建时间和更新时间的字段名. ================= ...
随机推荐
- PHP实现二分法查找
二分查找法需要数组是一个有序的数组. <?php function binarySearch($num, $arr) { $start = 0; $end = count($arr); $mid ...
- 自定义简单版本python线程池
python未提供线程池模块,在python3上用threading和queue模块自定义简单线程池,代码如下: #用threading queue 做线程池 import queue import ...
- 【机器学习】Google机器学习工程的43条最佳实践
https://blog.csdn.net/ChenVast/article/details/81449509 本文档旨在帮助那些掌握机器学习基础知识的人从Google机器学习的最佳实践中获益.它提供 ...
- Elasticsearch Search APIs
Elasticsearch Search APIs By:授客 QQ:1033553122 1. 搜索 1 在单个索引的所有类型中搜索 1 在单个索引的指定类型中搜索 1 在多个指定的索引中搜索 1 ...
- Android项目实战(四十二):启动页优化,去除短暂白屏或黑屏
大家会发现一个空项目,从手机桌面打开app是秒启动.但是对于自己开发的项目,有时会发现打开app的时候,会有短暂的1秒--2秒的白屏或者黑屏,然后才进入到程序界面. 个人理解为我们自己实现的Appli ...
- 如何在数据表当中找出被删掉的数据行ID
这个问题是一年前我刚步入IT行业的一个面试题,当时抓破头皮都想不到的问题,但现在回想过去自身不禁感到可笑,不多扯直接写解决方案.如何在数据表当中找出被删掉的数据行ID,意思是:在一堆的数据当中,让你找 ...
- Android 线程交互
在Android开发过程中,耗时操作是不允许写在主线程(UI线程)中的,以免由于等待时间过长而发生ANR.所以耗时操作需要创建子线程来完成,然而往往这些操作都需要与主线程进行通讯交互(例如更新主线程的 ...
- java读取excel文件的两种方式
方式一: 借用 package com.ij34.util; /** * @author Admin * @date 创建时间:2017年8月29日 下午2:07:59 * @version 1.0 ...
- iis 限制动态IP地址访问次数
An IP Address Blocking HttpModule for ASP.NET in 9 minutes namespace YourModuleNameHere 10 { 11 publ ...
- Javascript 高级程序设计--总结【四】
******************************* Chapter 11 DOM扩展 ******************************* 主要的扩展是 选择符API 和 H ...