public class DateTimeUtil {private DateTimeUtil() {}/*** 时间格式:yyyy-MM-dd HH:mm:ss*/public static final String TIME_FROMAT_SIMPLE1 = "yyyy-MM-dd HH:mm:ss";/*** 时间格式:yyyy/MM/dd HH:mm:ss*/public static final String TIME_FROMAT_SIMPLE2 = "yyyy/MM/dd HH:mm:ss";/*** 时间格式:yyyyMMddHHmmss*/public static final String TIME_FROMAT_SIMPLE3 = "yyyyMMddHHmmss";/*** 时间格式:yyyy年MM月dd日 HH点mm分ss秒*/public static final String TIME_FROMAT_SIMPLE4 = "yyyy年MM月dd日 HH点mm分ss秒";/*** 时间格式:yyyyMMddHH*/public static final String TIME_FROMAT_SIMPLE5 = "yyyyMMddHH";/*** 时间格式:HH:mm:ss*/public static final String TIME_FROMAT_HHMMSS = "HH:mm:ss";/*** 日期格式:yyyyMMdd*/public static final String DATE_FROMAT_YYYYMMDD1 = "yyyyMMdd";/*** 日期格式:yyyy-MM-dd*/public static final String DATE_FROMAT_YYYYMMDD2 = "yyyy-MM-dd";/*** 日期格式:yyyymm*/public static final String DATE_FROMAT_YYYYMM = "yyyyMM";public static final String DATE_FROMAT_SIMPLE6 = "yyyy-MM-dd HH:mm:ss.SSS";public static final String TIME_FROMAT_MIN = "yyyy-MM-dd HH:mm";/*** 日期格式:MM月dd日*/public static final String DATE_FROMAT_MMdd = "MM月dd日";/*** 日期格式:HH:mm*/public static final String TIME_FROMAT_HHMM = "HH:mm";/*** 时间转成 yyyy-MM-dd HH:mm:ss.SSS 格式** @return yyyy-MM-dd HH:mm:ss.SSS格式字符串*/public static String localDateTimeToString(LocalDateTime localDateTime) {DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(DATE_FROMAT_SIMPLE6);return formatDate.format(localDateTime);}/*** 时间转成 yyyy/MM/dd HH:mm:ss格式** @return yyyy/MM/dd HH:mm:ss 字符串*/public static String localDateTimeToString2(LocalDateTime localDateTime) {DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(TIME_FROMAT_SIMPLE2);return formatDate.format(localDateTime);}/***  LocalDateTime时间转成 yyyyMMdd格式** @return yyyyMMdd 字符串*/public static String localDateTimeToString1(LocalDateTime localDateTime) {DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(DATE_FROMAT_YYYYMMDD1);return formatDate.format(localDateTime);}/*** LocalDate  转换成yyyyMMdd 类型的字符串** @param localDate* @return*/public static String localDateToString(LocalDate localDate) {DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(DATE_FROMAT_YYYYMMDD1);return formatDate.format(localDate);}/*** 获取当前日期** @return yyyyMMdd 格式字符串*/public static final String getCurrentDay() {return dateToString(new Date(), DATE_FROMAT_YYYYMMDD1);}/*** DateToString** @param date* @param formatStr* @return*/public static final String dateToString(Date date, String formatStr) {SimpleDateFormat sf = new SimpleDateFormat(formatStr);return sf.format(date);}/*** yyyy-MM-dd格式字符串 转换为LocalDate* @param date* @return*/public static final LocalDate stringToDate(String date) {return LocalDate.parse(date, DateTimeFormatter.ofPattern(DATE_FROMAT_YYYYMMDD2));}/*** 按指定格式对LocalDate类型进行格式化输出(返回类型:String)** @param date* @return  yyyy-MM-dd格式字符串*/public static String localDateFormat(LocalDate date) {DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(DATE_FROMAT_YYYYMMDD2);return date.format(formatDate);}/*** 按指定格式对LocalTime类型进行格式化输出(返回类型:String)* HH:mm:ss* @param date* @return 返回时分秒*/public static String localTimeFormat(LocalTime date) {DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(TIME_FROMAT_HHMMSS);return date.format(formatDate);}/*** LocalDateTime 类型 转换成yyyy-MM-dd HH:mm:ss 格式字符串** @param date 时间* @return*/public static String localTimeFormat(LocalDateTime date) {DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(TIME_FROMAT_SIMPLE1);return date.format(formatDate);}/*** 时间字符串转化为指定格式的时间** @param dateStr    被转换的时间字符串* @param dateFormat 指定格式的字符串* @return LocalDate*/public static LocalDate parseString2LocalDate(String dateStr, String dateFormat) {DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);try {return LocalDate.parse(dateStr, dtf);} catch (Exception e) {log.error("-----parse String to LocalDate error!------dateStr:{}", dateStr, e);return null;}}/*** 时间字符串转化为指定格式的时间** @param dateStr    被转换的时间字符串* @param dateFormat 指定格式的字符串* @return LocalDate*/public static LocalDateTime parseString2LocalDateTime(String dateStr, String dateFormat) {DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);try {return LocalDateTime.parse(dateStr, dtf);} catch (Exception e) {log.error("-----parse String to LocalDateTime error!------dateStr:{}", dateStr, e);return null;}}/*** 时间字符串转换成yyyy-MM-dd HH:mm:ss格式的LocalDateTime** @param dateTime 时间参数* @return LocalDateTime*/public static LocalDateTime parseDateTime(String dateTime) {try {return LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(TIME_FROMAT_SIMPLE1));} catch (Exception e) {log.error("-----parse String to LocalDateTime error!------dateTimeStr:{}", dateTime, e);}return null;}/*** LocalDateTime转换为所需格式的String类型时间** @param localDateTime 被格式化的时间* @param formatStr     时间格式* @return String*/public static String formatLocalDateTime(LocalDateTime localDateTime, String formatStr) {DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatStr);return localDateTime.format(dtf);}/*** 计算两LocalDateTime间相差的秒数** @param startDateTime 起始时间* @param endDateTime   结束时间* @return 总秒数*/public static Long toSeconds(LocalDateTime startDateTime, LocalDateTime endDateTime) {return Duration.between(startDateTime, endDateTime).getSeconds();}/*** 计算两LocalDateTime间相差的秒数** @param startDate 起始时间* @param endDate   结束时间* @return 总秒数*/public static int toDays(LocalDate startDate, LocalDate endDate) {return Period.between(startDate, endDate).getDays();}/*** 比较所给时间dateTime是否在两时间点(startDateTime,endDateTime)之间** @param dateTime      被比较时间* @param startDateTime 时间点上限* @param endDateTime   时间点下限* @return Boolean true/false  若startDateTime<dateTime<endDateTime 则返回true,否则返回false*/public static Boolean isBetween(LocalDateTime dateTime, LocalDateTime startDateTime, LocalDateTime endDateTime) {return dateTime.isAfter(startDateTime) && dateTime.isBefore(endDateTime);}/*** LocalDate转换为LocalDateTime** @param localDate* @return*/public static LocalDateTime localDateToLocalDateTime(LocalDate localDate) {Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)return instant.atZone(zoneId).toLocalDateTime();}/*** LocalDate类型转换成yyyy-MM-dd HH:mm:ss 格式字符串** @param localDate localDate* @return 字符串结果*/public static String dateStrToLocalDateTime(LocalDate localDate) {LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);return localDateTime.format(DateTimeFormatter.ofPattern(TIME_FROMAT_SIMPLE1));}/*** yyyy-MM-dd 格式字符串转换为LocalDateTime** @param source* @return*/public static LocalDateTime parseStringToLocalDateTime(String source) {DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FROMAT_YYYYMMDD2);LocalDate localDate = LocalDate.parse(source, dateTimeFormatter);return localDate.atStartOfDay();}//" 02:03"public static final LocalDateTime stringMinToLocalDateTime(String min, String date) {DateTimeFormatter dtf = DateTimeFormatter.ofPattern(TIME_FROMAT_MIN);try {if (min.length() > 6) {return LocalDateTime.parse(min, dtf);} else {String dated = LocalDate.parse(date, DateTimeFormatter.ofPattern(DATE_FROMAT_YYYYMMDD1)).format(DateTimeFormatter.ofPattern(DATE_FROMAT_YYYYMMDD2));return LocalDateTime.parse(dated + " " + min, dtf);}} catch (Exception e) {log.error("-----parse String to LocalDateTime error!------dateStr:{}", min, e);return null;}}/*** 毫秒级时间戳转 LocalDateTime* @param epochMilli 毫秒级时间戳* @return LocalDateTime*/public static LocalDateTime ofEpochMilli(long epochMilli){return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneOffset.of("+8"));}}

Java8中关于LocalDateTime转换方法总结相关推荐

  1. dozer无法映射java8中的LocalDateTime类型的解决方案

    dozer是一个很方便的bean映射工具包,可以很轻松地做到两个bean对象的属性值复制,但是dozer包在2014年的时候停止更新了,而jdk1.8也是2014年发布的,所以对于java8中的新日期 ...

  2. Java8中的LocalDateTime获取当天的开始和结束时间

    /*** 获取当天的00:00:00* * * @return*/public static LocalDateTime getDayStart(LocalDateTime time) {return ...

  3. Java8中 Date和LocalDateTime的相互转换

    转载自  Java8中 Date和LocalDateTime的相互转换 一.在Java 8中将Date转换为LocalDateTime 方法1: 将Date转换为LocalDatetime,我们可以使 ...

  4. java8中LocalDate、LocalTime、LocalDateTime介绍

    很久以前java8中就推出了新的Time API,旨在解决旧版Date和Calendar的缺陷.讲道理真的挺好用的,不过由其他工具对新版time的兼容并不够完善,导致现在使用还不够普及.大家都还在用老 ...

  5. 一文搞懂Java8中表示当前的时间类Date、Instant、LocalDateTime、ZonedDateTime

    1. 概述 Java8中的时间类主要有:Date.Instant.LocalDateTime(LocalDate.LocalTime).ZonedDateTime,除去Date,java.time包下 ...

  6. Java8 ,LocalDate,LocalDateTime处理日期和时间工具类,

    Java8 ,LocalDate,LocalDateTime处理日期和时间工具类 日期格式化 1.获取今天的日期 2.在Java 8 中获取年.月.日信息 3.在Java 8 中处理特定日期 4.在J ...

  7. Java8中Lambda表达式的10个例子

    Java8中Lambda表达式的10个例子  例1 用Lambda表达式实现Runnable接口 Java代码   //Before Java 8: new Thread(new Runnable() ...

  8. 【Java8新特性】关于Java8中的日期时间API,你需要掌握这些!!

    写在前面 Java8之前的日期和时间API,存在一些问题,比如:线程安全的问题,跨年的问题等等.这些问题都在Hava8中的日期和时间API中得到了解决,而且Java8中的日期和时间API更加强大.立志 ...

  9. 静态方法:关于Java8中的日期时间API,你需要掌握这些!!

    Java8之前的日期和时间API,存在一些问题,比如:线程安全的问题,跨年的问题等等.这些问题都在Hava8中的日期和时间API中得到了解决,而且Java8中的日期和时间API更加强大.立志成为架构师 ...

  10. Java8中Stream流对集合操作

    java8中Stream流引入函数式编程思想,主要配合各种接口.lambda表达式.方法引用等方式,为集合的遍历.过滤.映射等提供非常"优雅"的操作方式. Student.java ...

最新文章

  1. 人脸识别引擎SeetaFaceEngine中Detection模块使用的测试代码
  2. LSTM之父:吐槽了两年,来划划重点:“这5篇最高引论文都源于我们。”
  3. win7讲述人安装包_文件夹选项与讲述人
  4. 安卓SDK之YUV-Image
  5. 你知道WPF这三大模板实例运用吗?
  6. Java Hashtable get()方法与示例
  7. react 子传参父_React 子组件向父组件传值的方法
  8. c#拼图碎片形状_使用神经网络解决拼图游戏
  9. 8大原则带你秒懂Happens-Before原则
  10. 在unity用shaderGraph做出类似动物之森的柱面场景,甚至球面场景。
  11. mongodb 监控分析命令
  12. 图像局部特征(四)--FAST-ER角点检测子
  13. 简单介绍.Net3.0 中跨线程访问控件
  14. Unity 动态更改鼠标样式
  15. (1)外网映射(NATAPP快速入门)
  16. java计算机毕业设计文档资料管理系统源码+系统+数据库+lw文档+mybatis+运行部署
  17. 映目云摄影以人脸识别惊艳Party现场,比亚迪元EV周年庆生
  18. Edit Control响应全选(Ctrl+A)
  19. debussy下载及安装
  20. Leetcode刷题笔记12:【20】有效的括号【155】最小栈【255】用队列实现栈(STL:stackC++ 单引号和双引号)

热门文章

  1. 官方文档-Linux服务器集群系统(一)
  2. 阿里云《云中谁送锦书来》活动 知识问答 答案
  3. Builder模式的误区
  4. 常见的会员积分系统都有什么样的功能?
  5. python 写命令行_一个用python写的用命令行看糗百的小工具
  6. AI遇上农业会怎样?最新UNT《智慧农业》2022全面综述农业4.0发展的架构、技术、应用等
  7. 第十七届全国大学智能汽车竞赛竞速比赛规则
  8. 阿朱说:咨询的历史(万字深度长文)
  9. 小学生必背古诗70首
  10. 专升本C语言必刷编程题