操作时间的工具类

public class DateUtil {

    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";





    /**
     * 判断时间字符串是否是在当前天以及当前天之后
     * 先将时间字符串转换成Date
     * 获取当前天的日期
     */
    public static boolean isCurAndNextDay(String str, String format) {

        boolean isCurAndNext=false;

        Date date = str2Date(str,format);
        Date curdate =getCurrentDate();

        long time=date.getTime();

        long curtime=curdate.getTime();

        if(curtime>=time){
            isCurAndNext=false;
        }else{
            isCurAndNext=true;
        }

        return isCurAndNext;

    }


    /**
     * 判断当前是一个星期中的第几天
     * 第一天是星期一 最后一天是星期日
     */
    public static int getWeekOfCurDate() {

        int w=0;

        Date curdate =getCurrentDate();

        Calendar cal = Calendar.getInstance();

        cal.setTime(curdate);

        w=cal.get(Calendar.DAY_OF_WEEK)-2;

        if(w<0){
            w=6;
        }
        return w;

    }


    /**
     * 将时间字符串转换成Date
     */
    public static Date str2Date(String str, String format) {
        if (str == null || str.length() == 0) {
            return null;
        }
        if (format == null || format.length() == 0) {
            format = FORMAT;
        }
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.parse(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;

    }

    public static Date str2Date(String str) {
        return str2Date(str, null);
    }

    /**
     * 将时间字符串转换成Calendar
     *
     * @param str
     * @param format
     * @return
     */
    public static Calendar str2Calendar(String str, String format) {

        Date date = str2Date(str, format);
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c;

    }

    public static Calendar str2Calendar(String str) {
        return str2Calendar(str, null);

    }

    public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
        return date2Str(c, null);
    }

    public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
        return date2Str(d, null);
    }

    public static String date2Str(Calendar c, String format) {
        if (c == null) {
            return null;
        }
        return date2Str(c.getTime(), format);
    }


    public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
        if (d == null) {
            return null;
        }
        if (format == null || format.length() == 0) {
            format = FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = sdf.format(d);
        return s;
    }

    /**
     * 获得当前日期的字符串格式
     * 2016-05-01
     *
     * @return
     */
    public static String getCurDateStr() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
                + c.get(Calendar.DAY_OF_MONTH) + "-"
                + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)
                + ":" + c.get(Calendar.SECOND);
    }

    /**
     * 获得当前日期的字符串格式
     */
    public static String getCurDateStr(String format) {
        Calendar c = Calendar.getInstance();
        return date2Str(c, format);
    }

    /**
     * 获得当前日期的字符串格式,格式到秒
     *
     * @return time -> yyyy-MM-dd-HH-mm-ss
     */
    public static String getMillon(long time) {

        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);

    }

    /**
     * 格式到天
     *
     * @return time -> yyyy-MM-dd
     */
    public static String getDay(long time) {

        return new SimpleDateFormat("yyyy-MM-dd").format(time);

    }

    /**
     * 格式到毫秒
     *
     * @return time -> yyyy-MM-dd-HH-mm-ss-SSS
     */
    public static String getSMillon(long time) {

        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);

    }


    /**
     * 输入的是String,格式诸如20120102,实现加一天的功能,返回的格式为String,诸如20120103
     */
    public static String strAddOneDay(String str) throws ParseException {
        String year = str.substring(0, 4);
        String month = str.substring(4, 6);
        String day = str.substring(6);
        String date1 = year + "-" + month + "-" + day;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(date1);
        Calendar cd = Calendar.getInstance();
        cd.setTime(startDate);
        cd.add(Calendar.DATE, 1);
        String dateStr = sdf.format(cd.getTime());
        String year1 = dateStr.substring(0, 4);
        String month1 = dateStr.substring(5, 7);
        String day1 = dateStr.substring(8);
        return year1 + month1 + day1;
    }

    /**
     * 输入的是String,格式诸如20120102,实现减一天的功能,返回的格式为String,诸如20120101
     */
    public static String strDecreaseOneDay(String row) throws ParseException {
        String year = row.substring(0, 4);
        String month = row.substring(4, 6);
        String day = row.substring(6);
        String date1 = year + "-" + month + "-" + day;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(date1);
        Calendar cd = Calendar.getInstance();
        cd.setTime(startDate);
        cd.add(Calendar.DATE, -1);
        String dateStr = sdf.format(cd.getTime());
        String year1 = dateStr.substring(0, 4);
        String month1 = dateStr.substring(5, 7);
        String day1 = dateStr.substring(8);
        return year1 + month1 + day1;
    }

    /**
     * 输入的格式为String,诸如20120101,返回的格式为String,诸如2012-01-01
     */
    public static String stringDateChange(String date) {
        if (date.length() == "20120101".length()) {
            String year = date.substring(0, 4);
            String month = date.substring(4, 6);
            String day = date.substring(6);
            return year + "-" + month + "-" + day;
        } else {
            return date;
        }


    }

    /**
     * 获取当前时间的Date
     *
     * @return
     */
    public static Date getCurrentDate() {

        Date date = new Date(getCurrentTime());


        return date;

    }

    /**
     * 获取当前时间的毫秒数
     *
     * @return
     */
    public static long getCurrentTime() {
        return System.currentTimeMillis();
    }

    /**
     * 获取昨天 Data
     *
     * @param date
     * @return
     */
    public static Date getLastdayDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * 获取当前时间的相差多天的日期集合 如;当前日期之前15天内的日期
     *
     * @param day 正 负 值
     * @return
     */
    public static List<Date> getDiffDayDate(int day) {

        List<Date> list = new ArrayList<>();



        for (int i = 0; i < Math.abs(day); i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(getCurrentDate());
            if (day > 0) {
                calendar.add(Calendar.DATE, (i + 1));
            } else {
                calendar.add(Calendar.DATE, -(i + 1));
            }

            list.add(calendar.getTime());
        }


        return list;


    }

    /**
     * 获取明天Date
     *
     * @param date
     * @return
     */
    public static Date getNextdayDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 判断是否是同一天
     *
     * @param one
     * @param another
     * @return
     */
    public static boolean isTheSameDay(Date one, Date another) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(one);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(another);
        int oneDay = calendar.get(Calendar.DAY_OF_YEAR);
        int anotherDay = calendar2.get(Calendar.DAY_OF_YEAR);
        return oneDay == anotherDay;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
日期操作工具类 /** * 将日期对象按照换为时间字符串 * 默认格式为"yyyy-MM-dd HH:mm:ss" * @param sourceTime * @return * @throws ParseException */ public static String formatDate(Date sourceDate) /** * 将日期对象按照指定格式转换为时间字符串 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static String formatDate(Date sourceDate, String formatStr) /** * 将时间字符串按照指定格式转换为时间字符串 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static String formatDate(String sourceTime, String formatStr) /** * 得到当前时间指定天数之前几天的日期 * @param days 指定的天数 * @return */ public static Date getFormerDateOfDays(int days) /** * 得到当前时间之前几个小时的日期 * @param hours 指定的小时数 * @return */ public static Date getFormerDateOfHours(int hours){} /** * 得到当前时间之前多少秒的日期 * @param sec 指定的秒数 * @return */ public static Date getFormerDateOfSecond(int sec){ } /** * 得到指定日期之前sec秒的日期 * @param days 指定的天数 * @return */ public static Date getFormerDateOfSecond(Date date, int sec){ } /** * 将时间字符串转换为日起对象 * 默认格式为“yyyy-MM-dd HH:mm:ss” * “yyyy/MM/dd HH:mm:ss”也可以 * @param sourceTime * @return * @throws ParseException */ public static Date paresDate(String sourceTime) throws ParseException { } /** * 返回指定月份的最后一天 * @param year * @param month * @return */ public static int getLastDayOfMonth(int year, int month){ } /** * 计算两个日期间的秒数 * @param beginDate * @param endDate * @return * @throws ParseException */ public static long getSecondBetweenDate(String beginDate, String endDate) throws ParseException { } /** * 将时间字符串按照指定格式转换为日期对象 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static Date paresDate(String sourceTime, String formatStr) throws ParseException { }
Hutool是一个Java开源工具类库,其中包含了丰富的常用工具类,提供了一套规范的工具类,使得开发更加简化和高效。其中也包括了时间工具类。Hutool的时间工具类主要是针对日期和时间相关的操作提供了一些便捷的方法。你可以使用Hutool的DateUtil工具类来进行日期和时间的处理。 使用Hutool的DateUtil工具类,你可以进行以下操作: 1. 获取当前日期和时间; 2. 格式化日期和时间; 3. 解析字符串为日期和时间对象; 4. 比较两个日期或时间的大小; 5. 进行日期和时间的加减运算; 6. 设置日期和时间的指定部分(如年、月、日、小时、分钟等); 7. 获取日期和时间的指定部分(如年、月、日、小时、分钟等)。 通过引入Hutool的依赖,你可以在你的项目中使用Hutool的时间工具类。在pom.xml文件中,添加以下依赖: ```xml <dependencies> <dependency> *** </dependency> </dependencies> ``` 然后,你可以使用Hutool的DateUtil工具类来进行日期和时间的处理。具体的使用方法可以参考Hutool的官方文档和API参考。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [HuTool从入门到精通1-日期和文件工具类入门](https://blog.csdn.net/weixin_44480609/article/details/125330109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值