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. BZOJ2384:[CEOI2014]Match

    浅谈\(KMP\):https://www.cnblogs.com/AKMer/p/10438148.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...

  2. Eclipse 创建类时添加继承

    eclipse 中类的继承创建有两种方式: 1.手动敲代码通过 extends 关键字来继承 public class A extends B { } 2.在创建类的时候就选择好继承关系 点击 Fin ...

  3. Unit08: Spring集成mybatis

    Unit08: Spring集成mybatis 1. Spring集成mybatis (1)方式一 step1. 导包. spring-webmvc,mybatis,mybatis-spring, o ...

  4. (转)Linq DataTable的修改和查询

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. 将h264和aac码流合成flv文件

    在视频应用中,经常需要将接收到h264和aac数据保存成文件. 本来想用mp4格式,但是mp4在没有正常关闭的情况下会导致文件打不开,而在实际应用中经常会出现设备直接拔电,程序不是正常结束的情况.于是 ...

  6. python 面向对象(三大特性)

    python 面向对象(初级) (思维导图 ↑↑↑↑↑) 概述: 面向过程:根据业务逻辑从上到下垒代码. 函数式:将某功能代码封装至函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类 ...

  7. java代码---------打印正三角形

    总结:手打一遍 public class aaa{ public static void main(String []args){ for(int i=1;i<10;i++){ for(int ...

  8. Java 代码块

    一.简介 1.普通代码块: 类中方法的方法体 2.构造代码块: 构造块会在创建对象时被调用,每次创建时都会被调用,优先于类构造函数执行. 3.静态代码块: 用static{}包裹起来的代码片段,只会执 ...

  9. 用命令行方式关闭CentOS防火墙

    #/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #/sbin/iptables -I INPUT -p tcp --dport 22 -j A ...

  10. 分析java类的初始化契机

    分析java类的静态成员变量初始化先于非静态成员变量   依上图中当class字节码文件被jvm虚拟机加载到内存中依次经过 连接 验证:对字节码进行验证 准备:给静态变量分配内存并赋予变量类型各自的默 ...