LocalDateTime&LocalDate&LocalTime
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);
LocalDateTime ldt = LocalDateTime.parse("2019-07-10 17:07:05",df);
System.out.println("LocalDateTime转成String类型的时间:"+localTime);
System.out.println("String类型的时间转成LocalDateTime:"+ldt);
# day hours minutes
LocalDateTime localDateTime3 = LocalDateTime.now();
LocalDateTime localDateTime4 = LocalDateTime.now();
Duration duration = Duration.between(localDateTime3,ldt);
System.out.println("day:"+duration.toDays() +"hours"+ duration.toHours()+"minutes:"+ duration.toMinutes()); #
Period period2 = Period.between(localDateTime4.toLocalDate(),localDateTime4.toLocalDate());
period2.getYears();
period2.getMonths();
period2.toTotalMonths();
#plusDays
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startTime = LocalDateTime.now();
LocalDateTime endTime = startTime.plusDays(7);
boolean isSameYear = startTime.getYear() ==endTime.getYear();
boolean isSameMonth =startTime.getMonth()==endTime.getMonth();
boolean isSameDate =startTime.getDayOfYear()==endTime.getDayOfYear();
System.out.println("开始时间:" + startTime.format(df) + ",结束时间:" + endTime.format(df));
LocalDateTime dateTest = LocalDateTime.parse("2018-02-30 12:12:12", df);
System.out.println("时间自动转化:" + dateTest.toString());
int daysNum = Period.between(startTime.toLocalDate(), endTime.toLocalDate()).getDays();
int monthNum = Period.between(startTime.toLocalDate(), endTime.toLocalDate()).getMonths();
System.out.println("相差天数:" + daysNum);
System.out.println("相差月数:" + monthNum);
System.out.println("当前时间向前推6天:" + LocalDateTime.now().minusDays(6));
System.out.println("当前时间向前推6小时:" + LocalDateTime.now().minusHours(6));
System.out.println(LocalDate.now().until(LocalDate.now().minusDays(5), ChronoUnit.MONTHS));
//查询两个LocalDate的相差天数
System.out.println(LocalDate.now().toEpochDay() - LocalDate.now().minusDays(5).toEpochDay());
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
//int day = localDateTime.getDayOfYear();
//只获取年月日
System.out.println("当前月份为:" + localDateTime.getYear());
System.out.println("当前月份为:" + localDateTime.getMonthValue());
System.out.println("当前日期为:" + localDateTime.getDayOfMonth());
System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
LocalDate ld = LocalDate.parse("2018-09-26");
LocalDateTime ldt = LocalDateTime.parse("2018-09-26T22:24:33");
//当前时间在结束日期之前
Boolean is = LocalDateTime.now().isBefore(endTime);
//当前时间在开始时间之后
Boolean is2 = LocalDateTime.now().isAfter(startTime);
long DAYS= betweenTwoTime(startTime,endTime,ChronoUnit.DAYS); //SECONDS
long SECONDS= betweenTwoTime(startTime,endTime,ChronoUnit.SECONDS);
//查询两个LocalDate的相差天数
System.out.println(endTime.toLocalDate().toEpochDay() - startTime.toLocalDate().toEpochDay());
// Java 8
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate date2 = LocalDate.now();
System.out.println(date2.format(newFormatter));
/**
* 获取两个日期的差 field参数为ChronoUnit.*
* @param startTime
* @param endTime
* @param field 单位(年月日时分秒)
* @return
*/
public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
if (field == ChronoUnit.YEARS) return period.getYears();
if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
return field.between(startTime, endTime);
}
List<Integer> list = Arrays.asList(10, 20, 30, 40); List<Integer> result1= list.stream().sorted(Comparator.comparingInt((x)->(int)x).reversed()).collect(Collectors.toList());
System.out.println(result1);
LocalDateTime&LocalDate&LocalTime的更多相关文章
- jdk8中LocalDateTime,LocalDate,LocalTime等日期时间类
package com.zy.time; import org.junit.Test; import java.time.*; import java.time.format.DateTimeForm ...
- LocalDate、LocalDateTime、LocalTime开发小结
在我之前的文章<[整理]Java 8新特性总结 >中有提到Date/Time API (JSR 310)对日期与时间的处理.它将服务端对时间的处理进行了统一,使得对时间的处理更加规范和统一 ...
- LocalDate LocalTime LocalDateTime Instant的操作与使用
一.简介 LocalDate表示当前(或指定)日期,格式为:yyyy-MM-dd LocalTime表示当前(或指定)时间,格式为:HH:mm:ss SSS LocalDateTime表示当前(或指定 ...
- Java 时间类-Calendar、Date、LocalDate/LocalTime
1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...
- (转载)Java8新的日期API LocalDate, LocalTime
前言 由于Java Date的各种问题,Java8推出了新的日期API,很受一拨人的追捧. 为什么我们需要新的Java日期/时间API? 在开始研究Java 8日期/时间API之前,让我们先来看一下为 ...
- springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)--thymeleaf格式化LocalDateTime,LocalDate等JDK8新时间类
依赖maven包 <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>th ...
- SrpingMVC通过JSON注入from数据到实体自定义(LocalDateTime,LocalDate,Boolean类型)字段的序列化、反序列化方法
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingExcept ...
- mybatis低版本jsr310(LocalDateTime,LocalDate等) Joda Time支持
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybati ...
- Spring Boot(十二):LocalDateTime格式化处理
Java 8之后,日期类的处理建议使用java.time包中对应的LocalDateTime, LocalDate, LocalTime类.(参考Java8新特性) 在Spring Boot中(验证版 ...
随机推荐
- SpringBoot 为API添加统一的异常处理(一)
首先我把异常分为两种,一种是可控制的,或者是由我们发现条件不正确主动抛出的异常,就像前城市编号不存在那个粟子:另一种是不可控制的,或者说是程序存在bug引起的异常,但这种异常也不想变态的就直接给前端抛 ...
- 大数据 Hibernate
大数据 Hibernate - 国内版 Binghttps://cn.bing.com/search?FORM=U227DF&PC=U227&q=%E5%A4%A7%E6%95%B0% ...
- C# Mysql 查询 Rownum
SELECT @rownum:=@rownum+1 AS rownum,a.order_id ,case when a.Ordered =1 then '已分单' end as Ordered,a.p ...
- CentOS7安装Apache2和PHP7
安装Apache 2.4 更新源:rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmrpm ...
- ospf的路由更新和撤销总结
首先ospf 的报文有:hello报文,主要作用ospf 邻居建立及维护.dd报文,主要作用主从选举,序列号主从的确认,mtu的协商(可选).lsr 报文,主要作用向邻居请求lsa.lsu报文,主要作 ...
- PHP MQTT 实践
MQTT介绍:http://mqtt.org 服务器端https://mosquitto.org/download/ PHP客户端https://github.com/bluerhinos/phpMQ ...
- 路径规划: PRM 路径规划算法 (Probabilistic Roadmaps 随机路标图)
随机路标图-Probabilistic Roadmaps (路径规划算法) 路径规划作为机器人完成各种任务的基础,一直是研究的热点.研究人员提出了许多规划方法如: 1. A* 2. Djstar 3. ...
- docker 启动mysql 闪退 无法启动问题
docker 安装mysql [获取容器] docker pull mysql:5.6 [启动容器] docker run -p 3306:3306 --name mymysql -v $PWD/co ...
- Vmware解决虚拟机不能联网的问题
1. 设置为NAT模式 2. 启动win7的服务,命令窗口输入services.msc 回车 3. 修改Vmware的设置
- React全家桶+Material-ui构建的后台管理系统
一.简介 一个使用React全家桶(react-router-dom,redux,redux-actions,redux-saga,reselect)+Material-ui构建的后来管理中心. 二. ...