iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java日期工具类操作字符串Date和LocalDate互转
  • 893
分享到

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

2024-04-02 19:04:59 893人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录字符串转DateDate转字符串字符串转LocalDateDate转LocalDateLocalDate转字符串两个日期的时间差一天的开始和结束时间工具类前言: 避免重复造轮子,

前言:

避免重复造轮子,相关方法基于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);

工具类


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");

    
    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);
    }

    
    public static String date2Str(Date date) {
        return date2Str(null, date);
    }

    
    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);
    }

    
    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);
    }

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

    
    public static String localTime2Str(LocalTime localTime) {
        return localTime2Str(null, localTime);
    }

    
    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);
    }
    
    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);
    }
   
    public static LocalDate date2LocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
    
    public static String localDate2Str(LocalDate localDate) {
        return localDate2Str(null, localDate);
    }

    
    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);
    }
    
    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);
    }

    
    public static LocalDateTime date2LocalDateTime(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    
    public static String localDateTime2Str(LocalDateTime localDateTime) {
        return localDateTime2Str(null, localDateTime);
    }
    
    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互转内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java日期工具类操作字符串Date和LocalDate互转

本文链接: https://www.lsjlt.com/news/151486.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作