package com.zy.time;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set; public class TestTimeAPI { /**
* LocalDate、LocalTime、LocalDateTime
* LocalDate专门表示日期
* LocalTime专门表示时间
* LocalDateTime可以同时表示日期和时间
*
*/ // 1.基本年月日,时分秒及当前时间的获取:人所读的
@Test
public void fn1(){
// 1.获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println("1.获取当前时间=========="+now);
// 2.设置任意时间
LocalDateTime ldt = LocalDateTime.of(,,,,,);
System.out.println("2.设置任意时间==============="+ldt);
// 3.增加或减少年月日,时分秒
LocalDateTime ldt2 = ldt.plusYears();
System.out.println(ldt2);
ldt.minusMonths();
// 4.获取年月日,时分秒
System.out.println(ldt.getYear());
System.out.println(ldt.getMonth());
System.out.println(ldt.getDayOfMonth());
System.out.println(ldt.getHour());
System.out.println(ldt.getMinute());
System.out.println(ldt.getSecond());
// 获取毫秒见fn2
System.out.println(ldt.getNano());
System.out.println(ldt.getDayOfWeek());
System.out.println(ldt.getDayOfYear());
} // 2.Instant:时间戳:计算机所读的时间(使用 Unix 元年 1970年1月1日 00:00:00 所经历的毫秒值)
@Test
public void fn2(){
Instant now = Instant.now();
System.out.println(now);
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours());
System.out.println(offsetDateTime);
System.out.println(now.toEpochMilli());
System.out.println(now.getNano());
Instant instant = Instant.ofEpochSecond();
System.out.println(instant);
} // 3.Duration : 用于计算两个“时间”间隔
@Test
public void fn3() throws InterruptedException {
Instant start = Instant.now();
Thread.sleep();
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
System.out.println("Duration==============="+duration.toMillis());
LocalTime start1 = LocalTime.now();
Thread.sleep();
LocalTime end1 = LocalTime.now();
Duration duration1 = Duration.between(start1, end1);
System.out.println("Duration==============="+duration1.toMillis());
} // 4.Period : 用于计算两个“日期”间隔
@Test
public void fn4() throws InterruptedException {
LocalDate begin = LocalDate.of(, , );
LocalDate end = LocalDate.now();
Period period = Period.between(begin, end);
System.out.println(period.getYears()+"年"+period.getMonths()+"月"+period.getDays()+"日");
} // 5.TemporalAdjuster : 时间校正器
@Test
public void fn5(){
LocalDateTime now = LocalDateTime.now();
// 修改至某月
LocalDateTime ldt2 = now.withMonth();
System.out.println(ldt2); // 获取下一个周日的日期
LocalDateTime ldt3 = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(ldt3); //自定义:下一个工作日
LocalDateTime with = now.with((x) -> {
LocalDateTime localDateTime = (LocalDateTime) x;
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return localDateTime.plusDays();
} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
return localDateTime.plusDays();
} else {
return localDateTime.plusDays();
}
});
System.out.println(with);
} // 6. DateTimeFormatter : 解析和格式化日期或时间
@Test
public void fn6(){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String format = dateTimeFormatter.format(now);
String format1 = now.format(dateTimeFormatter);
System.out.println(format);
System.out.println(format1); LocalDateTime parse = now.parse(format1, dateTimeFormatter);
System.out.println(parse); } // 7.ZonedDate、ZonedTime、ZonedDateTime : 带时区的时间或日期
@Test
public void fn7(){
Set<String> set = ZoneId.getAvailableZoneIds();
set.forEach(System.out::println);
} @Test
public void fn8(){
LocalDateTime ldt = LocalDateTime.now((ZoneId.of("Asia/Hong_Kong")));
System.out.println(ldt);
} }

jdk8中LocalDateTime,LocalDate,LocalTime等日期时间类的更多相关文章

  1. jdk8环境下sprngboot/springmvc中JSR310新日期/时间类LocalDateTime显示效果带T

    如图所示: 日期时间类中带了一个T,以上这种格式LocalDateTime格式化的时候默认日期时间格式:ISO.DATE_TIME(按笔者目前的知识理解是ISO8601规范中的日期时间格式化) 想要把 ...

  2. 详解 JDK8 新增的日期时间类

    JDK8 新增的日期时间类 在本人之前的博文<处理时间的类 -- System类.Date类 .SimpleDateFormat类 与 Calendar类>中,讲到过表示时间的类,有三类: ...

  3. 【JDK8】Java8 LocalDate操作时间和日期的API

    时间项目中的涉及到的时间处理非常多,犹豫SimpleDateFormat的不安全性以及Calendar等类在计算时比较复杂, 往往我们都会使用工具类来封装较多的日期处理函数, 但是JDK8中新增了操作 ...

  4. Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例

    Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...

  5. 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结

    1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018             年月日时分秒    CST代表北 ...

  6. 日期时间类:Date,Calendar,计算类:Math

    日期时间类 计算机如何表示时间? 时间戳(timestamp):距离特定时间的时间间隔. 计算机时间戳是指距离历元(1970-01-01 00:00:00:000)的时间间隔(ms). 计算机中时间2 ...

  7. Java日期时间类

    日期时间类有三种: 一.java.util.Date:一般用于声明日期时间类型的变量. 二.java.sql.Date:一般用于数据库日期时间的映射. 三.java.util.Calendar:一般用 ...

  8. Java日期时间API系列3-----Jdk7及以前的日期时间类的不方便使用问题

    使用Java日期时间类,每个人都很熟悉每个项目中必不可少的工具类就是dateutil,包含各种日期计算,格式化等处理,而且常常会遇到找不到可用的处理方法,需要自己新增方法,处理过程很复杂. 1.Dat ...

  9. Java 之 JDK1.8之前日期时间类

    一.JDK1.8之前日期时间类 二. java.lang.System类 System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1 ...

随机推荐

  1. AT指令(二)

    1.常用操作1.1 AT命令解释:检测 Module 与串口是否连通,能否接收 AT 命令:命令格式:AT<CR>命令返回:OK (与串口通信正常)             (无返回,与串 ...

  2. JVM内存管理之垃圾搜集器简介

    引言 上一章我们已经探讨过GC的各个算法,那么垃圾搜集器是什么呢? 通俗的讲,使用编程语言将算法实现出来,产生的程序就是垃圾搜集器了.既然谈到了编程语言的实现,那么在讨论垃圾搜集器的时候,就已经涉及到 ...

  3. 批处理实现mysql的备份

    脚本 @echo off echo. echo MySQL数据库备份 echo ***************************** echo. echo 今天是 %date% echo 时间是 ...

  4. android视频处理相关资料

    <开源>基于Android的点对点视频通信/RTSP/RTP/H.264 http://blog.csdn.net/cazicaquw/article/details/8650543 历经 ...

  5. Python基础之元组tuple(带了枷锁的列表)

    元组不能直接插入删除修改元素. 逗号是关键 创建空元组 元组的一些操作符 别忘了逗号,"*"重复操作符也可以使用 元组通过切片间接更新 插入元素,可以使用拼接操作符(“+”) &g ...

  6. MapReduce启动的Map/Reduce子任务简要分析

      对于Hadoop来说,是通过在DataNode中启动Map/Reduce java进程的方式来实现分布式计算处理的,那么就从源码层简要分析一下hadoop中启动Map/Reduce任务的过程.   ...

  7. node的close

    在http.ServerResponse对象的end方法被调用之前,如果连接被中断,将触发http.ServerResponse对象的close事件. var http=require("h ...

  8. Python basic (from learn python the hard the way)

    1. How to run the python file? python ...py 2. UTF-8 is a character encoding, just like ASCII. 3. ro ...

  9. Tkinter Relief styles(样式)

      Tkinter Relief styles: 构件的浮雕式是指某些模拟的3-D周围的部件外的影响.下面是一排按钮的屏幕截图展示了所有可能的救济风格   构件的浮雕式是指某些模拟的3-D周围的部件外 ...

  10. python第三方库推荐 - dateutil

    在dateutil中,吸引我的东西有2个,1个是parser,1个是rrule. 其中parser是根据字符串解析成datetime,而rrule是则是根据定义的规则来生成datetime. 安装 没 ...