Java日期工具类操作字符串Date和LocalDate互转

 更新时间:2022年06月15日 08:59:07   作者:​ 共饮一杯无   ​  
这篇文章主要介绍了Java日期工具类操作字符串Date和LocalDate互转,文章首先通过需要先引入坐标展开主题的相关内容介绍,需要的朋友可以参一下

前言:

避免重复造轮子,相关方法基于hutool日期时间工具封装并做部分增强。需要先引入如下坐标

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.4.7</version>
</dependency>          

字符串转Date

//字符串转Date
Date dateTime = DateUtil.parseDate("2022-04-06");
System.out.println(dateTime);

Date转字符串

//Date转字符串不指定format格式,默认yyyy-MM-dd HH:mm:ss
String dateStr = DateUtil.date2Str(new Date());
System.out.println(dateStr);
//Date转字符串指定格式
String dateStr2 = DateUtil.date2Str("yyyy/MM/dd",new Date());
System.out.println(dateStr2);

字符串转LocalDate

//字符串转LocalDate
LocalDate localDate = DateUtil.parseLocalDate("2022-04-06");
System.out.println(localDate);

Date转LocalDate

//Date转LocalDate
LocalDate localDate = DateUtil.date2LocalDate(new Date());
System.out.println(localDate);

LocalDate转字符串

//LocalDate转Str
String localDateStr = DateUtil.localDate2Str(LocalDate.now());
System.out.println(localDateStr);

两个日期的时间差

String beginDateStr = "2022-02-01 22:33:23";
Date beginDate = DateUtil.parse(beginDateStr);
String endDateStr = "2022-03-10 23:33:23";
Date endDate = DateUtil.parse(endDateStr);
//相差天数(37)
long betweenDay = DateUtil.between(beginDate, endDate, DateUnit.DAY);
System.out.println(betweenDay);
//格式化时间差(37天1小时)
String formatBetween = DateUtil.formatBetween(beginDate, endDate, BetweenFormater.Level.HOUR);
System.out.println(formatBetween);

一天的开始和结束时间

String dateStr = "2022-04-07 10:33:23";
Date date = DateUtil.parse(dateStr);

//一天的开始时间:2022-04-07 00:00:00
Date beginOfDay = DateUtil.beginOfDay(date);
System.out.println(beginOfDay);

//一天的结束时间:2022-04-07 23:59:59
Date endOfDay = DateUtil.endOfDay(date);
System.out.println(endOfDay);

工具类

/**
 * <p>基于hutool的日期工具类增强</p>
 *
 * @author zjq
 * @date 2021/04/07
 */
public class DateUtil extends cn.hutool.core.date.DateUtil {

    private static final String[] PARSE_PATTERNS = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 字符串转date类型
     *
     * @param dateStr
     * @return
     */
    public static Date parseDate(Object dateStr) {
        if (ObjectUtils.isNull(dateStr)) {
            return null;
        }
        if (dateStr instanceof Double) {
            return org.apache.poi.ss.usermodel.DateUtil.getJavaDate((Double) dateStr);
        }
        return parse(dateStr.toString(), PARSE_PATTERNS);
    }

    /**
     * Date类型转字符串
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String date2Str(Date date) {
        return date2Str(null, date);
    }

    /**
     * Date类型转字符串
     *
     * @param format
     * @param date
     * @return
     */
    public static String date2Str(String format, Date date) {
        if (ObjectUtils.isNull(date)) {
            return null;
        }
        SimpleDateFormat dateFormat = StrUtil.isNotBlank(format) ?new SimpleDateFormat(format):
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    /**
     * 字符串转LocalTime类型
     *
     * @param dateStr
     * @return
     */
    public static LocalTime parseLocalTime(Object dateStr) {
        if (ObjectUtils.isNull(dateStr)) {
            return null;
        }
        if (dateStr instanceof Double) {
            return date2LocalTime(parseDate(dateStr));
        }
        return LocalTime.parse(dateStr.toString(), TIME_FORMATTER);
    }

    /**
     * Date转LocalTime
     *
     * @param date
     * @return
     */
    public static LocalTime date2LocalTime(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    }

    /**
     * LocalTime转Str
     *
     * @param localTime
     * @return HH:mm:ss
     */
    public static String localTime2Str(LocalTime localTime) {
        return localTime2Str(null, localTime);
    }

    /**
     * LocalTime转str
     *
     * @param format    格式
     * @param localTime
     * @return
     */
    public static String localTime2Str(String format, LocalTime localTime) {
        if (null == localTime) {
            return null;
        }
        DateTimeFormatter formatter = StrUtil.isBlank(format) ?
                TIME_FORMATTER : DateTimeFormatter.ofPattern(format);
        return localTime.format(formatter);
    }
    /**
     * 字符串转LocalDate类型
     *
     * @param dateStr
     * @return
     */
    public static LocalDate parseLocalDate(Object dateStr) {
        if (ObjectUtils.isNull(dateStr)) {
            return null;
        }
        if (dateStr instanceof Double) {
            return date2LocalDate(parseDate(dateStr));
        }
        return LocalDate.parse(dateStr.toString(), DATE_FORMATTER);
    }
   /**
     * Date转LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate date2LocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
    /**
     * LocalDate转Str
     *
     * @param localDate
     * @return
     */
    public static String localDate2Str(LocalDate localDate) {
        return localDate2Str(null, localDate);
    }

    /**
     * LocalDate转Str
     *
     * @param format    格式
     * @param localDate
     * @return
     */
    public static String localDate2Str(String format, LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        DateTimeFormatter formatter = StrUtil.isBlank(format) ?
                DATE_FORMATTER : DateTimeFormatter.ofPattern(format);
        return localDate.format(formatter);
    }
    /**
     * 字符串转LocalDateTime类型
     *
     * @param dateStr
     * @return
     */
    public static LocalDateTime parseLocalDateTime(Object dateStr) {
        if (ObjectUtils.isNull(dateStr)) {
            return null;
        }
        if (dateStr instanceof Double) {
            return date2LocalDateTime(parseDate(dateStr));
        }
        return LocalDateTime.parse(dateStr.toString(), DATETIME_FORMATTER);
    }

    /**
     * Date转LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    /**
     * LocalDate转Str
     *
     * @param localDateTime
     * @return
     */
    public static String localDateTime2Str(LocalDateTime localDateTime) {
        return localDateTime2Str(null, localDateTime);
    }
    /**
     * LocalDate转Str
     *
     * @param format
     * @param localDateTime
     * @return
     */
    public static String localDateTime2Str(String format, LocalDateTime localDateTime) {
        if (null == localDateTime) {
            return null;
        }
        DateTimeFormatter formatter = StrUtil.isBlank(format) ?
                DATETIME_FORMATTER : DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }
}

到此这篇关于Java日期工具操作字符串Date和LocalDate互转的文章就介绍到这了,更多相关Date和LocalDate互转内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot的http.server.requests服务请求流程源码

    springboot的http.server.requests服务请求流程源码

    这篇文章主要为大家介绍了springboot的http.server.requests服务请求流程源码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • SpringAOP 如何通过JoinPoint获取参数名和值

    SpringAOP 如何通过JoinPoint获取参数名和值

    这篇文章主要介绍了SpringAOP 通过JoinPoint获取参数名和值的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Java Spring Cloud 负载均衡详解

    Java Spring Cloud 负载均衡详解

    这篇文章主要介绍了Spring Cloud负载均衡及远程调用实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09
  • Java 的双重分发与 Visitor 模式实例详解

    Java 的双重分发与 Visitor 模式实例详解

    这篇文章主要介绍了Java 的双重分发与 Visitor 模式实例详解,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • SpringBoot整合MyBatis超详细教程

    SpringBoot整合MyBatis超详细教程

    这篇文章主要介绍了SpringBoot整合MyBatis超详细教程,下面从配置模式、注解模式、混合模式三个方面进行说明MyBatis与SpringBoot的整合,需要的朋友可以参考下
    2021-05-05
  • springboot实现excel表格导出几种常见方法

    springboot实现excel表格导出几种常见方法

    在日常的开发中避免不了操作Excel,下面这篇文章主要给大家介绍了关于springboot实现excel表格导出的几种常见方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • java理论基础Stream reduce实现集合元素归约

    java理论基础Stream reduce实现集合元素归约

    这篇文章主要为大家介绍了java理论基础Stream reduce实现集合元素归约示例详解有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03
  • java正则表达式验证工具类

    java正则表达式验证工具类

    这篇文章主要介绍了java正则表达式验证工具类,可以校验电话号码,身份证号码,日期格式,URL,email等等,需要的小伙伴参考下吧。
    2015-03-03
  • java 两个数组合并的几种方法

    java 两个数组合并的几种方法

    本篇文章主要介绍了java 两个数组合并的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Java调用linux shell脚本的方法

    Java调用linux shell脚本的方法

    这篇文章主要介绍了Java调用linux shell脚本的方法,需要的朋友可以参考下
    2015-02-02

最新评论