1. package com.java8.date;
  2.  
  3. import org.junit.Test;
  4.  
  5. import java.text.SimpleDateFormat;
  6. import java.time.*;
  7. import java.time.format.DateTimeFormatter;
  8. import java.time.temporal.*;
  9.  
  10. public class DateTest {
  11.  
  12. @Test
  13. public void LocalDateTest() {
  14.  
  15. // of方法获取一个指定日期的LocalDate
  16. LocalDate date1 = LocalDate.of(2018, 12, 29);
  17. System.out.println(date1);
  18. System.out.println(date1.getYear());
  19. System.out.println(date1.getMonth());
  20.  
  21. System.out.println(date1.getMonthValue());
  22. System.out.println(date1.getDayOfMonth());
  23. System.out.println(date1.getDayOfWeek());
  24. System.out.println(date1.getDayOfWeek().getValue());
  25. System.out.println(date1.getDayOfYear());
  26.  
  27. System.out.println("判断时间前后关系:" + date1.isBefore(date1));
  28.  
  29. // now 获取当前时间
  30. System.out.println(LocalDate.now());
  31.  
  32. // 获取指定字段的值
  33. System.out.println(date1.get(ChronoField.YEAR));
  34. System.out.println(date1.get(ChronoField.MONTH_OF_YEAR));
  35. System.out.println(date1.get(ChronoField.DAY_OF_YEAR));
  36. System.out.println(date1.get(ChronoField.DAY_OF_MONTH));
  37.  
  38. // 多了一些加减运算
  39. //Peroid是针对日期的 , Duration 主要是针对Time的
  40. System.out.println(date1.minus(Period.ofYears(2)));
  41. System.out.println(date1.minus(Period.ofDays(2)));
  42. System.out.println(date1.minus(Period.ofWeeks(2)));
  43.  
  44. Period between = Period.between(date1.minus(Period.ofYears(2)), date1);
  45. Period between2 = Period.between(date1.minus(Period.ofMonths(2)), date1);
  46.  
  47. System.out.println(between.getMonths());
  48. System.out.println(between2.getMonths());
  49. System.out.println("date1.minus(between) = " + date1.minus(between));
  50.  
  51. LocalDate now = LocalDate.now();
  52. // 替换年
  53. System.out.println(now.withYear(2016));
  54.  
  55. // 计算某一个时间字段的取值范围
  56. System.out.println(now.range(ChronoField.DAY_OF_MONTH));
  57. }
  58.  
  59. @Test
  60. public void LocalTimeTest() {
  61.  
  62. LocalTime now = LocalTime.now();
  63. System.out.println(now);
  64.  
  65. LocalTime time = LocalTime.of(12, 12);
  66. System.out.println(time);
  67.  
  68. System.out.println(time.isBefore(now));
  69. // 由于是time所以不支持年字段
  70. System.out.println(time.isSupported(ChronoField.YEAR));
  71. System.out.println(time.isSupported(ChronoUnit.YEARS));
  72. System.out.println(time.isSupported(ChronoUnit.HOURS));
  73.  
  74. System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss")));
  75.  
  76. // Duration 主要是针对Time的,Peroid是针对日期的
  77. System.out.println(" time.minus(Duration.ofHours(2)) = " + time.minus(Duration.ofHours(2)));
  78. System.out.println(" time.plus(Duration.ofHours(2)) = " + time.plus(Duration.ofHours(2)));
  79.  
  80. }
  81.  
  82. @Test
  83. public void LocalDateTimeTest() {
  84.  
  85. LocalDateTime dateTime = LocalDateTime.now();
  86. //2018-12-29T13:38:03.212
  87. System.out.println(dateTime.toString());
  88.  
  89. dateTime = LocalDateTime.of(2018, 12, 29, 12, 12);
  90.  
  91. System.out.println(dateTime);
  92.  
  93. //指定今天的12点12分
  94.  
  95. LocalDateTime toDay1212 = LocalDate.now().atTime(LocalTime.of(12, 12));
  96. System.out.println(toDay1212);
  97.  
  98. //DateTime转Date
  99. System.out.println(toDay1212.toLocalDate());
  100. //DateTime转Time
  101. System.out.println(toDay1212.toLocalTime());
  102. // 替换某一个时间单位
  103. System.out.println(toDay1212.withHour(14));
  104.  
  105. }
  106.  
  107. @Test
  108. public void instantTest() {
  109.  
  110. System.out.println(Instant.now());
  111. System.out.println(System.currentTimeMillis());
  112. System.out.println(System.nanoTime());
  113. System.out.println(Instant.now().toEpochMilli());
  114. System.out.println(Instant.now().plusNanos(0));
  115. System.out.println(Instant.now().get(ChronoField.NANO_OF_SECOND));
  116.  
  117. Period between = Period.between(LocalDate.of(2018, 12, 26), LocalDate.of(2018, 12, 28));
  118.  
  119. System.out.println(between.getDays());
  120. // between表示是一个间隔,做加减法时候只拿间隔做运算,不考虑具体的起止日期
  121. System.out.println(LocalDateTime.now().plus(between));
  122.  
  123. }
  124.  
  125. @Test
  126. public void TemporalAdjusterTest() {
  127.  
  128. // 计算当前日期的加一个周五
  129. System.out.println(LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
  130. // 计算该月的最后一天
  131. System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()));
  132. // 计算该年的最后一天
  133. System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()));
  134.  
  135. }
  136.  
  137. @Test
  138. public void DateTimeFormatterTest() {
  139. //DateTimeFormatter 线程安全线的 ,SimpleDateFormat线程不安全原因是底层公用一个Calender成员
  140. System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
  141.  
  142. }
  143.  
  144. @Test
  145. public void ZoneIdTest() {
  146.  
  147. System.out.println(ZoneId.systemDefault());
  148.  
  149. }
  150. }

  

Java8时间的简单时间的更多相关文章

  1. 详解Java8的日期和时间API

    详解Java8的日期和时间API 在JDK1.0的时候,Java引入了java.util.Date来处理日期和时间:在JDK1.1的时候又引入了功能更强大的java.util.Calendar,但是C ...

  2. Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法:

    Qt中利用QTime类来控制时间,这里简单介绍一下QTime的成员函数的用法: ------------------------------------------------------------ ...

  3. Python3.x:简单时间调度Timer(间隔时间执行)

    Python3.x:简单时间调度Timer(间隔时间执行) threading模块中的Timer能够帮助实现定时任务,而且是非阻塞的: 代码: import threading import time ...

  4. 减少Qt编译时间暨简单Qt裁剪

    本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作.原文链接:减少Qt编译时间暨简单Qt裁剪 编译一 ...

  5. 都9012了,Java8中的日期时间API你还没有掌握?

    一,Java8日期时间API产生的前因后果 1.1 为什么要重新定义一套日期时间API 操作不方便:java中最初的Date不能直接对指定字段进行加减操作也不支持国际化,后来新增了Calendar,但 ...

  6. linux 时间处理 + 简单写log

    1s ==1000ms == 1,000,000us == 1,000,000,000 nanosecond uname -a Linux scott-Z170X 4.15.0-34-generic ...

  7. Linux系统时间与RTC时间【转】

    http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3637782 Linux的RTC驱动相对还是比较简单的,可以将它作为一个普通的字符 ...

  8. ftp上来显示的时间和系统时间不一致

    ftp上来显示的时间和系统时间不一致,是因为默认情况下,vsftpd 是用GMT做为他的时间的,所以和系统的时间可能会不一致 修改也非常简单: vi /etc/vsftpd/vsftpd.conf 在 ...

  9. Laravel / Lumen 框架修改 创建时间 和 更新时间 对应字段

    为避免浪费时间--先上解决方案 在Model中重写 CREATED_AT 和 UPDATED_AT 两个类常量就可以了,这两个常量分别是创建时间和更新时间的字段名. ================= ...

随机推荐

  1. GET和POST两种基本请求方法的区别(转)

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  2. java中获取路径的方法

    在class获取路径的方法,getResource有没有“\”的区别 System.out.println("" + this.getClass().getResource(&qu ...

  3. Eureka开启登录认证

    Eureka服务端配置 一.Eureka的pom.xml 引入spring-boot-starter-security坐标 <dependency> <groupId>org. ...

  4. Laravel条件查询数据单条数据first,多条数据get

    使用DB查询,必须use Illuminate\Support\Facades\DB; 多数组条件查询单条数据 first() //提交加入我们数据 public function ajax_join ...

  5. Java判断一个字符是否是数字的几种方法的代码

    在工作期间,将写内容过程经常用到的一些内容段做个记录,下面内容是关于Java判断一个字符是否是数字的几种方法的内容,希望能对码农们有好处. public class Test{ public stat ...

  6. mysql之连接查询、联合查询、子查询

    本文内容: 连接查询 联合查询 子查询 from子查询 where子查询 exists子查询 首发日期:2018-04-11 连接查询: 连接查询就是将多个表联合起来查询,连接查询方式有内连接.外连接 ...

  7. ssh框架总结之action接收参数的三种方式

    页面将参数传递给action的三种方式 一是通过属性传值: 将页面和action的的属性值保持一致,在action上写上该属性的set和get方法,这样在页面提交参数的时候,action就会调用set ...

  8. 简易付弹窗问题FAQ

    情景:按返回键后,使用快捷键弹到数据统计页面. 适用门店类型:单纯收银. 解决方案:操作步骤下                        1.       系统修复-更多-快捷键设置 . 2.   ...

  9. 卸载时候出现: windows installer 程序有问题。此安装需要的dll不能运行 的一个解决方法

    卸载时候出现: windows installer 程序有问题.此安装需要的dll不能运行 安装Your Uninstaller来卸载

  10. scrapy入门:安装scrapy

    1.安装Scrapy pip 安装: pip install scrapy 要求pip至少是18.1版本的,10.x版本会提示更新pip 更新pip命令: python -m pip install ...