首页 > 其他分享 >时间工具类Dateutil

时间工具类Dateutil

时间:2022-10-19 17:03:52浏览次数:55  
标签:Dateutil 00 return long 时间 cal Date Calendar 工具

目录

格式

2021-08-10

YYYY_MM_DD = "yyyy-MM-dd"

获取参数日期的凌晨12点,时间

点击查看代码
 /**
     * 获取参数日期的凌晨12点,时间字符
     *
     * @param date 2021-08-10 14:57:37
     * @return 示例:2021-08-10 00:00:00
     */
    public static Date getDateForZero(Date date){
        String format = DateFormatUtils.format(date, YYYY_MM_DD);
        return DateUtils.parseDate(format);
    }

获取参数日期的凌晨12点,时间字符(日数字不变)

点击查看代码
/**
     * 获取参数日期的凌晨12点,时间字符
     *
     * @param date 2021-08-10 14:57:37
     * @return 示例:2021-08-10 00:00:00
     */
    public static String getDateForZeroString(Date date){
        String format = DateFormatUtils.format(date, YYYY_MM_DD);
        /*DateUtils.YYYY_MM_DD;*/
        Date newDate = DateUtils.parseDate(format);

        return DateFormatUtils.format(newDate, YYYY_MM_DD_HH_MM_SS);
    }

获取2个日期的时间差(几天几小时几分)

点击查看代码
  /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        if(day>0) {
            return day + "天" + hour + "小时" + min + "分钟";
        }else if(hour>0){
            return hour + "小时" + min + "分钟";
        }
        return min + "分钟";
    }

获取当前时间相差n天的凌晨00:00:00时间

点击查看代码
/**
     * 获取当前时间相差天数的凌晨00:00:00时间
     * @param diffDay
     * @return
     * @author liuruqi
     */
    public static Date getNowDiffDate(int diffDay){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, diffDay);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

根据日期获取年龄

点击查看代码
/**
     * 根据日期获取生日
     * @param birthDay
     * @return
     */
    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            return 0;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth)
                    age--;
            } else {
                age--;
            }
        }

        return age;
    }

标签:Dateutil,00,return,long,时间,cal,Date,Calendar,工具
From: https://www.cnblogs.com/jf666/p/16806871.html

相关文章

  • 正则工具
    /***@param:[soap,rgex]*@description:返回指定字符中间的字符串*/publicstaticStringgetSubUtilSimple(Stringsoap,Stringrgex){......
  • 数据库连接池-druid-工具类和数据库连接池-druid-工具类测试
    数据库连接池-druid-工具类定义工具类:1.定义一个类JDBCUtils2.提供静态代码块加载配置文件,初始化连接池对象3.提供方法1.获取连接方法:通过数据库连接池获......
  • Sql server根据时间最新的去除重复显示到前面
    话不多说看效果:这原来的数据是有重复的,除了Id和创建时间不太相同以外,其他都是一模一样的。(什么业务需求不重要,重要的是怎么去实现需求)sql:select*from(select*,ROW......
  • Keytool 工具的介绍与使用
    Keytool工具的介绍与使用keytool简介Keytool是一个Java数据证书的管理工具,Keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中。keystore文......
  • Vue学习笔记之el-date-picker组建时间格式化方式
    <el-date-pickersize="small"v-model="editData.startTime"type="datetime"placeholder="选择日期时间"value-format="yyyy-MM-ddHH:mm:ss"format="yyyy-MM-ddHH:......
  • 两个Date类型计算时间差
    参考: Java----日期算法(计算两个date类型的时间差) ......
  • Apple开发_获取文件创建时间
    /***@brief获取文件创建时间**@paramfile_path文件所在的绝对路径*@return文件创建时间*/-(NSDate*)file_Creat_Date:(NSString*)file_path{NS......
  • Centos8中如何使用Chrony同步时间
    在Linux服务器中如果我们希望所有Linux服务器都具有正确的时间,则必须配置一些NTP客户端,该客户端将始终从远程NTP服务器获取正确的时间。安装chrony使用下面​​命令​​​在......
  • FFmpeg中转场滤镜xfade的时间参数(duration和offset)与算法解读
    xfade转场滤镜小科普最近在研究音视频合成的相关功能,现已有两个视频剪辑。拼合成一个文件显然用concat可以完成,但是过渡生硬,而xfade滤镜可以很方便实现更加缓和的场景切换。......
  • java == 时间日期类
    时间日期类--Date类计算机中时间原点1970年1月1日00:00:00时间换算单位1秒=1000毫秒Date类概述Date代表了一个特定的时间,精确到毫秒构造方法方法名......