LocalDate计算两个日期相差天数

摘要:
importorg.apache.commons.lang.StringUtils;importjava.time.LocalDate;importjava.time.ZoneId;importjava.time.format.DateTimeFormatter;importjava.time.temporal.ChronoUnit;importjava.util.Date;/***LocalDa
importorg.apache.commons.lang.StringUtils;


importjava.time.LocalDate;
importjava.time.ZoneId;
importjava.time.format.DateTimeFormatter;
importjava.time.temporal.ChronoUnit;
importjava.util.Date;

/*** LocalDate 工具类
 * @authorzsw
 * @date 2021-03-16 18:43
 */
public classLocalDateUtils {

    /*** 计算当前日期与{@codeendDate}的间隔天数
     *
     * @paramendDate
     * @return间隔天数
     */
    public static longlocalDateCompare(LocalDate endDate){
        returnLocalDate.now().until(endDate, ChronoUnit.DAYS);
    }

    /*** 计算日期{@codestartDate}与{@codeendDate}的间隔天数
     *
     * @paramstartDate
     * @paramendDate
     * @return间隔天数
     */
    public static longlocalDateCompare(LocalDate startDate, LocalDate endDate){
        returnstartDate.until(endDate, ChronoUnit.DAYS);
    }

    /*** 字符串转换成日期
     * @paramstrDate 日期字符串
     * @parampattern 日期的格式
     */
    public staticLocalDate stringToLocalDate(String strDate, String pattern) {
        if(StringUtils.isBlank(strDate)){
            return null;
        }
        DateTimeFormatter fmt =DateTimeFormatter.ofPattern(pattern);
        returnLocalDate.parse(strDate, fmt);
    }

    /*** Date转LocalDate
     * @paramdate
     */
    public staticLocalDate dateToLocalDate(Date date) {
        if(null ==date) {
            return null;
        }
        returndate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
}

免责声明:文章转载自《LocalDate计算两个日期相差天数》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MFC 多线程kubectl 常用命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

input 时间字段默认值

背景: 时间字段展示默认值,开始时间为当天 0点,结束时间为当天晚上12点 代码: 1 <input style="Width: 180px;float:left ;" type="date" id="start_time" 2 name="start_time" placeholder="请选择具体时间" data-format="YYY...

centos环境下使用percona-xtrabackup对mysql5.6数据库innodb和myisam进行快速备份及恢复

centos环境下使用percona-xtrabackup对mysql5.6数据库innodb和myisam进行快速备份及恢复 有时候我们会碰到这样的业务场景: 1.将大的数据库恢复到本地进行业务测试 2.数据库性能碰到了瓶颈需要做基于主从复制的读写分离 这样我们就需要对源数据库进行备份,并恢复到目标服务器中 如果源数据库全部是myisam数据库那我们可以...

Easyui的DateBox日期格式化

DateBox 日期显示默认的格式为“dd/mm/yyyy”,如果想自定义成我们的格式需要实现两个函数,formatter和parser。 formatter函数使得选择日期后将其格式化为我们需要的格式,parser函数在选择好日期后告诉控件如何去解析我们自定义的格式。 定义如下: formatter:A function to format the da...

修改unix服务器时间

如果只是需要修改时间 使用date -s 12:00:00即可,改动是即时的。 CODE: # dateThu Dec 16 12:00:00 CST 2010 注意后面的“CST”为时区,修改时区有几种方法。 假如不想重启的话,可以考虑修改bash环境,首先看一看,有没有这个TZ(timezone)变量。 #env |grep TZ 没有,很干净。 #c...

Oracle Database 11g Express Edition学习笔记

修改字符集 使用用户system,通过sqlplus程序连接到Oracle数据库,输入以下命令,查看字符集: SQL> select userenv('language') fromdual; USERENV('LANGUAGE') ---------------------------------------------------- AMERI...

Shell脚本中时间处理

Shell脚本中时间处理 1.脚本内容 #!/bin/bash #环境变量 #设置环境变量和sql文件格式相符 source /etc/profileexport LD_LIBRARY_PATH="/usr/lib/oracle/12.1/client64/lib" export NLS_LANG="AMERICAN_AMERICA.ZHS16GBK" e...