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的更多相关文章

  1. jdk8中LocalDateTime,LocalDate,LocalTime等日期时间类

    package com.zy.time; import org.junit.Test; import java.time.*; import java.time.format.DateTimeForm ...

  2. LocalDate、LocalDateTime、LocalTime开发小结

    在我之前的文章<[整理]Java 8新特性总结 >中有提到Date/Time API (JSR 310)对日期与时间的处理.它将服务端对时间的处理进行了统一,使得对时间的处理更加规范和统一 ...

  3. LocalDate LocalTime LocalDateTime Instant的操作与使用

    一.简介 LocalDate表示当前(或指定)日期,格式为:yyyy-MM-dd LocalTime表示当前(或指定)时间,格式为:HH:mm:ss SSS LocalDateTime表示当前(或指定 ...

  4. Java 时间类-Calendar、Date、LocalDate/LocalTime

    1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...

  5. (转载)Java8新的日期API LocalDate, LocalTime

    前言 由于Java Date的各种问题,Java8推出了新的日期API,很受一拨人的追捧. 为什么我们需要新的Java日期/时间API? 在开始研究Java 8日期/时间API之前,让我们先来看一下为 ...

  6. springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)--thymeleaf格式化LocalDateTime,LocalDate等JDK8新时间类

    依赖maven包 <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>th ...

  7. SrpingMVC通过JSON注入from数据到实体自定义(LocalDateTime,LocalDate,Boolean类型)字段的序列化、反序列化方法

    import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingExcept ...

  8. mybatis低版本jsr310(LocalDateTime,LocalDate等) Joda Time支持

    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybati ...

  9. Spring Boot(十二):LocalDateTime格式化处理

    Java 8之后,日期类的处理建议使用java.time包中对应的LocalDateTime, LocalDate, LocalTime类.(参考Java8新特性) 在Spring Boot中(验证版 ...

随机推荐

  1. MySql删除重复数据并保留一条

    DELETE FROM tbl_1 WHERE id NOT IN( SELECT id FROM ( SELECT min(id) AS id FROM tbl_1 GROUP BY `duplic ...

  2. tornado多进程模式不同进程写不同日志

    #coding: utf- ''' Author: Time: Target: ''' import logging import logging.handlers import os import ...

  3. 解决:File "/usr/lib/python2.7/site-packages/more_itertools/more.py", line 340 def _collate(*iterables, key=lambda a: a, reverse=False): 的报错

    cyberb commented on 15 Apr Traceback (most recent call last): File "/snap/users/x1/python/bin/l ...

  4. linux命令(55):环境变量:LIBRARY_PATH 和 LD_LIBRARY_PATH的区别

    添加环境变量:https://www.cnblogs.com/lovychen/p/5583703.html PATH是可执行文件的环境变量. LIBRARY_PATH 和 LD_LIBRARY_PA ...

  5. JAVA中使用LDAP登录的三种方式

    搜索中关于java 登录ldap,大部分会采用  cn=xxx,ou=xxx,dc=xxx的方式,此处的cn是用户的Display Name,而不是account,而且如果ou有多层,比如我们的OU就 ...

  6. 阿里云环境安装K8S步骤

    1. 安装docker yum install -y docker 2. 修改 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值 $ vim /et ...

  7. 【漫谈数据仓库】 如何优雅地设计数据分层 ODS DW DM层级

    转载http://bigdata.51cto.com/art/201710/554810.htm 一.文章主题 本文主要讲解数据仓库的一个重要环节:如何设计数据分层!其它关于数据仓库的内容可参考之前的 ...

  8. oracle 导出,导入表

    导出 exp DZQZ/DZQZ@orcl file=D:/DZQZ.dmp log=D:/DZQZ.log 导入 imp DZQZ/DZQZ@orcl file=D:\电子取证\DZQZ.dmp f ...

  9. Gulp 搭建前端非SPA 项目, 修改文件浏览器自动刷新

    起因:需要搭建一个自动打包处理 sass / js (es6),自动监听文件变化时浏览器自动刷新的开发环境 项目目录 project build -css -js *.html src -html - ...

  10. maven工程仿springboot手写代码区分开发测试生产

    读取代码: package com.jz.compute.mc.v2.config; import java.util.Enumeration; import java.util.ResourceBu ...