jdk8中LocalDateTime,LocalDate,LocalTime等日期时间类
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等日期时间类的更多相关文章
- jdk8环境下sprngboot/springmvc中JSR310新日期/时间类LocalDateTime显示效果带T
如图所示: 日期时间类中带了一个T,以上这种格式LocalDateTime格式化的时候默认日期时间格式:ISO.DATE_TIME(按笔者目前的知识理解是ISO8601规范中的日期时间格式化) 想要把 ...
- 详解 JDK8 新增的日期时间类
JDK8 新增的日期时间类 在本人之前的博文<处理时间的类 -- System类.Date类 .SimpleDateFormat类 与 Calendar类>中,讲到过表示时间的类,有三类: ...
- 【JDK8】Java8 LocalDate操作时间和日期的API
时间项目中的涉及到的时间处理非常多,犹豫SimpleDateFormat的不安全性以及Calendar等类在计算时比较复杂, 往往我们都会使用工具类来封装较多的日期处理函数, 但是JDK8中新增了操作 ...
- Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例
Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...
- 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结
1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018 年月日时分秒 CST代表北 ...
- 日期时间类:Date,Calendar,计算类:Math
日期时间类 计算机如何表示时间? 时间戳(timestamp):距离特定时间的时间间隔. 计算机时间戳是指距离历元(1970-01-01 00:00:00:000)的时间间隔(ms). 计算机中时间2 ...
- Java日期时间类
日期时间类有三种: 一.java.util.Date:一般用于声明日期时间类型的变量. 二.java.sql.Date:一般用于数据库日期时间的映射. 三.java.util.Calendar:一般用 ...
- Java日期时间API系列3-----Jdk7及以前的日期时间类的不方便使用问题
使用Java日期时间类,每个人都很熟悉每个项目中必不可少的工具类就是dateutil,包含各种日期计算,格式化等处理,而且常常会遇到找不到可用的处理方法,需要自己新增方法,处理过程很复杂. 1.Dat ...
- Java 之 JDK1.8之前日期时间类
一.JDK1.8之前日期时间类 二. java.lang.System类 System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1 ...
随机推荐
- 全局获取Context
1.定制一个Application类,管理全局的状态信息 public class MyApplication extends Application{ private static Context ...
- SpringMVC之八:基于SpringMVC拦截器和注解实现controller中访问权限控制
SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...
- maven环境的搭建,lemon-OA办公系统的搭建
当时要搭建activiti工作流,但是这个工作流是基于maven启动的,于是,学习了一下,maven环境的搭建 准备的环境: Jdk 1.6 Eclipse IDE 一个或者 MyEclipse M ...
- javascript数组的申明方式以及常用方法
数组的定义: 方法1. var mycars=new Array()mycars[0]="Saab"mycars[1]="Volvo"mycars[2]=&qu ...
- Python - 第一个 Django 项目
Django 的安装: pip3 install django==1.11.11 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ d ...
- Python 模块 - jieba
安装 jieba pip3 install jieba jieba 支持三种分词模式: 精确模式:将句子最精确地切开,适合文本分析 全模式:把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不 ...
- @RestController的方法中 路径参数带.(点号)配置
如下面这种//http://localhost:8080/api/v1/user/info/email/test@163.com @RequestMapping(value = "/info ...
- Log4j记录日志使用方法
1.导入相关JAR包 log4j-1.2.15.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar log4jdbc4-1.2.jar 2.配置log4j. ...
- 第6章Zabbix分布式监控
Zabbix是一个分布式的监控系统.分布式监控适合跨机房.跨地域的网络监控.从多个Proxy收集数据,而每个Proxy可以采集多个设备的数据,从而轻松地构建分布式监控系统. ZabbixProxy可以 ...
- ie6绝对定位的块会被select元素遮挡的解决方案
RT(已无力吐槽ie),解决方法是:定义一个iframe,与想要显示的绝对定位的块设置为同一大小.放在同一个位置上.我的网页里绝对定位的元素是会随着鼠标移动显示和隐藏的,于是这个frame也要跟着显示 ...