Java.Utils:Date 时间工具类

Java.Utils:Date 时间工具类packagecom.boob.common.utils;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;/***@description:…

大家好,又见面了,我是你们的朋友全栈君。

Don’t say much, just go to the code.

package org.bood.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/** * Date 时间工具类 * * @author bood * @since 2020/10/16 */
public class DateUtils { 
   

    private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

    private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

    private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");

    private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final static SimpleDateFormat sdfTimeString = new SimpleDateFormat("yyyyMMddHHmmss");

    private DateUtils() { 
   
    }
    
    /** * 获取 YYYY 格式 * * @return yyyy */
    public static String getYear() { 
   
        return sdfYear.format(new Date());
    }

    /** * 获取 YYYY-MM-DD 格式 * * @return YYYY-MM-DD */
    public static String getDay() { 
   
        return sdfDay.format(new Date());
    }

    /** * 获取 YYYYMMDD 格式 * * @return YYYYMMDD */
    public static String getDays() { 
   
        return sdfDays.format(new Date());
    }

    /** * 获取 YYYY-MM-DD HH:mm:ss 格式 * * @return YYYY-MM-DD HH:mm:ss */
    public static String getTime() { 
   
        return sdfTime.format(new Date());
    }

    /** * 获取 YYYYMMDDHHmmss 格式 * * @return YYYYMMDDHHmmss */
    public static String getTimeString() { 
   
        return sdfTimeString.format(new Date());
    }

    /** * <p> * 日期比较,如果s > = e 返回 true 否则返回 false * </p> * * @param s: * @param e: * @return:boolean * @author:bood * @date:2020/10/16 */
    public static boolean compareDate(String s, String e) { 
   
        if (fomatDate(s) == null || fomatDate(e) == null) { 
   
            return false;
        }
        return fomatDate(s).getTime() >= fomatDate(e).getTime();
    }

    /** * <p> * 格式化日期 * </p> * * @param date: * @return:java.util.Date * @author:bood * @date:2020/10/16 */
    public static Date fomatDate(String date) { 
   
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try { 
   
            return fmt.parse(date);
        } catch (ParseException e) { 
   
            e.printStackTrace();
            return null;
        }
    }

    /** * <p> * 校验日期是否合法 * </p> * * @param s: * @return:boolean * @author:bood * @date:2020/10/16 */
    public static boolean isValidDate(String s) { 
   
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try { 
   
            fmt.parse(s);
            return true;
        } catch (Exception e) { 
   
            // 如果 throw java.text.ParseException 或者 NullPointerException,就说明格式不对
            return false;
        }
    }

    /** * <p> * 时间相减得到年数 * </p> * * @param startTime: * @param endTime: * @return:int * @author:bood * @date:2020/10/16 */
    public static int getDiffYear(String startTime, String endTime) { 
   
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try { 
   
            int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
            return years;
        } catch (Exception e) { 
   
            // 如果 throw java.text.ParseException 或者 NullPointerException,就说明格式不对
            return 0;
        }
    }

    /** * <p> * 时间相减得到天数 * </p> * * @param beginDateStr: * @param endDateStr: * @return:long * @author:bood * @date:2020/10/16 */
    public static long getDaySub(String beginDateStr, String endDateStr) { 
   
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = null;
        Date endDate = null;

        try { 
   
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
        } catch (ParseException e) { 
   
            e.printStackTrace();
        }
        day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        // System.out.println("相隔的天数="+day);
        return day;
    }

    /** * <p> * 得到 n 天之后的日期 * </p> * * @param days: * @return:java.lang.String * @author:bood * @date:2020/10/16 */
    public static String getAfterDayDate(String days) { 
   
        int daysInt = Integer.parseInt(days);
        // java.util包
        Calendar canlendar = Calendar.getInstance();
        // 日期减 如果不够减会将月变动
        canlendar.add(Calendar.DATE, daysInt);
        Date date = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);

        return dateStr;
    }

    /** * 得到n天之后是周几 * * @param days * @return */
    public static String getAfterDayWeek(String days) { 
   
        int daysInt = Integer.parseInt(days);
        // java.util包
        Calendar canlendar = Calendar.getInstance();
        // 日期相减,如果不够减会将月变动
        canlendar.add(Calendar.DATE, daysInt);
        Date date = canlendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);

        return dateStr;
    }

    /** * <p> * 获得当前时间之前或之后几个月的日期 * </p> * * @param amount: 值可正可负,负<==>before 正<==>after * @return:java.lang.String yyyy-MM-dd HH:mm:ss * @author:bood * @date:2020/10/16 */
    public static String getAfterMonthDate(Integer amount) { 
   
        //获取当前时间
        Date nowDate = new Date();
        //获取日历类实例
        Calendar cl = Calendar.getInstance();
        //设置当前时间
        cl.setTime(nowDate);
        cl.add(Calendar.MONTH, amount);
        return sdfTime.format(cl.getTime());
    }

    /** * <p> * 获得当前时间之前或之后几年的日期 * </p> * * @param amount: 值可正可负,负<==>before 正<==>after * @return:java.lang.String yyyy-MM-dd HH:mm:ss * @author:bood * @date:2020/10/16 */
    public static String getAfterYearDate(Integer amount) { 
   
        //获取当前时间
        Date nowDate = new Date();
        //获取日历类实例
        Calendar cl = Calendar.getInstance();
        //设置当前时间
        cl.setTime(nowDate);
        cl.add(Calendar.YEAR, amount);
        return sdfTime.format(cl.getTime());
    }

}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160247.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 服务器如何存储图片(图片服务器搭建方案)

    参考:https://www.zhihu.com/question/20518854我们的ext4/dev/sda37299235847亿多个inode了。数据库直接存图片本身是SB的做法。比较好的方法是存图片md5,然后通过类似md52url获取图片的地址。至于图片存放,建议打散,打3级或5级。类似a/de/df/adedf***************.jpg…

    2022年4月10日
    334
  • unity3d自学教程_3D技巧

    unity3d自学教程_3D技巧0.目录简要介绍基本概念视图菜单坐标系统资源元素脚本交互相机操作 1.简要介绍Unity3D软件是由UnityTechnologies公司提供的综合开发环境,主要面向游戏开发人员、虚拟现实设计师等,可用于创建诸如三维视频游戏、建筑可视化、实时三维动画等类型的多媒体内容,并支持这些内容在Windows、iOS、Android等多种平台的发布,功能非常强大。Unity3D

    2022年8月10日
    12
  • BIST

    BISTBIST即是在设计时在电路中植入相关功能电路用于提供自我测试功能的技术,BIST把测试仪的部分功能转移到电路内部,用嵌入到电路中的测试电路提供输入测试向量和分析响应的功能,最后输出简单的测试结果。根据被测试的对象不同,BIST测试分为LogicBIST和MemoryBIST。LogicBIST原理结构:实现方法一般基于STUMPS结构,包含测试向量生成模块PRPG、响应分析模块M

    2025年8月20日
    5
  • god is a girl 俚语_God will make a way

    god is a girl 俚语_God will make a wayWehumanbeingsarealwaystryingtofindaeverythingoperationofthelaw.So,Ialsodon’tstrangemyselfsometimeswhythis.Thetruth

    2022年10月8日
    3
  • c语言实现冒泡排序算法_c语言如何实现动态数组

    c语言实现冒泡排序算法_c语言如何实现动态数组冒泡排序作为学习排序最基本的算法,具有稳定性与实用性。下面是C语言冒泡排序的源代码#include<stdio.h>intmain(void){inta[10]={6,4,3,2,7,8,9,10,1,5};inti,k,w;for(i=0;i<9;i++){for(k=0;k<9-i;k++){if(a[k]>a[k+1]){…

    2025年6月7日
    4
  • 生产环境数据库并发数的调整

    生产环境数据库并发数的调整在开发和测试时,我们往往不会很在意数据库相关的一些并发数的配置,因为开发和测试时,系统的并发量并不会很大,因此,是否正确设置这些参数也不会对结果造成什么影响但是,上生产环境后,系统的并发量大大提高,这时,没有注意数据库的并发数配置的话就会使数据库成为系统最终的并发瓶颈。根据我在实际项目中一段时间的并发测试后,发现关于数据库并发数需要配置的几个地方,希望跟大家分享一下,…

    2022年6月13日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号