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 两个类常量就可以了,这两个常量分别是创建时间和更新时间的字段名. ================= ...
随机推荐
- js正则表达式 URL格式匹配详解
0.URL格式 protocol :// hostname[:port] / path / [;parameters][?query]#fragment [;parameters]没见过 这里就不做相 ...
- 去除input[type=number]的默认样式
input[type=number] { -moz-appearance: textfield; } input[type=number]::-webkit-inner-spin-button, in ...
- jfinal框架学习过程
刚刚学习jfinal,通过一天左右的时间大体上理解了这个框架的用法,我对他的理解是JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速.代码量少.学习简单.功 ...
- IE CSS Hack【记录】
1.条件hack 2.属性hack 3.选择器hack CSS Hack一般都是利用各浏览器的支持CSS的能力和BUG来进行的 本文只列举了一些常用的CSS Hack,且不考虑IE6以下的版本 尽可能 ...
- xml可视化编辑器
——业内首创的在线可视化XML结构化数据编辑方法 Boxth Visual XML Web Editor (Boxth XWE) 是专为在线处理XML结构化数据而设计的在线(Web).可视化(WYSW ...
- 21.Odoo产品分析 (三) – 人力资源板块(2) – 工时表(1)
查看Odoo产品分析系列--目录 工时表是一个用来管理员工工作时间和出勤的模块.当需要计算员工的工作时间,并将这些时间对应到项目或者顾客上时,"工时表"就会起到一个非常好的作用. ...
- Python函数式编程(一):高级函数
首先有一个高级函数的知识. 一个函数可以接收另一个函数作为参数,这种函数就称之为高阶函数. def add(x, y, f): return f(x) + f(y) 当我们调用add(-, , abs ...
- vue 构建项目遇到的问题
1.我在打包完成后,打开index.html文件发现地址并没有携带路由. config下的 index.js 中的build命令的配置有一个属性叫assetsPublicPath,它的值为‘/’.意思 ...
- Android内存优化(五) Lint代码扫描工具
1.使用 工具栏 -> Analyze -> Inspect Code… 点击 Inspect Code 后会弹出检查范围的对话框: 默认是检查整个项目,我们可以点击 Custom sc ...
- leetcode-977. 有序数组的平方
leetcode-977. 有序数组的平方 (来自 120周赛) 题意 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序. 示例 1: 输入:[-4,-1 ...