前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java 日期格式化工具类

java 日期格式化工具类

作者头像
六月的雨在Tencent
发布2024-03-28 20:45:18
650
发布2024-03-28 20:45:18
举报
文章被收录于专栏:CSDNCSDN
java 日期格式化工具类

代码如下

代码语言:javascript
复制
package com.dongao.project.utils;

import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Version 1.0
 * @Date 2019-08-19
 **/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static final String YYYY = "yyyy";

    public static final String YYYY_MM = "yyyy-MM";

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYY_MM_DD_HH_MM = "yyyyMMddHHmm";

    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String MM_DD_HH_MM = "MM-dd HH:mm";

    private static String[] parsePatterns = {
            "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"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String getMmTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    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;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static String getFormatDate(String format,Date date) {
        if (null != date) {
            if (StringUtils.isEmpty(format)) {
                format = YYYY_MM_DD_HH_MM_SS;
            }
            String dateStr = parseDateToStr(format, date);
            return dateStr;
        }
        return "";
    }

    /**
     * 秒转小时分
     * @param second
     * @return
     */
    public static String getFormatTime(Long second) {

        if (second != null) {

            if(second < 3600) {//分

                return NumFormat(second / 60) + "分";
            }

            if(second< 3600 * 24) {//时

                return NumFormat(second/ 60 / 60) + "小时" + NumFormat(second/ 60 % 60) + "分";
            }

            if(second>= 3600 * 24) {//天

                return NumFormat(second/ 60 / 60 /24) + "天" +NumFormat(second/ 60 / 60 % 24) + "小时" + NumFormat(second/ 60 % 60) + "分";
            }
        }

        return "--";
    }

    /**
     * 格式化时间
     * @param sec
     * @return
     */
    private static String NumFormat(long sec) {
        if (String.valueOf(sec).length() < 2){
            return "0"+sec;
        }else {
            return String.valueOf(sec);
        }
    }

    /**
     * 获取当前时间前面某天或者后面某天
     * @param date
     * @return
     */
    public static Date getOtherDay(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH,amount);
        Date time = calendar.getTime();
        return time;
    }

    /**
     * 获取当前时间月日 7.13
     * @param date
     * @return
     */
    public static String getMonthDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //month从0开始
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return month+"."+day;
    }
    /**
     * 获取周几
     * @param date
     * @return
     */
    public static String getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        String week = null;
        if (weekday == 1) {
            week = "周日";
        } else if (weekday == 2) {
            week = "周一";
        } else if (weekday == 3) {
            week = "周二";
        } else if (weekday == 4) {
            week = "周三";
        } else if (weekday == 5) {
            week = "周四";
        } else if (weekday == 6) {
            week = "周五";
        } else if (weekday == 7) {
            week = "周六";
        }
        return week;
    }

    /**
     * 根据日期字符串获取时间
     * @param str
     * @return
     */
    public static Date getDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        Date parse = null;
        try {
            parse = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
    /**
     * 根据日期字符串获取时间
     * @param format
     * @param str
     * @return
     */
    public static Date getDate(String format,String str) {
        if (StringUtils.isEmpty(format)) {
            format = YYYY_MM_DD_HH_MM_SS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date parse = null;
        try {
            parse = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 格式化日期
     * @param format
     * @param date
     * @return
     */
    public static Date getChangeDate(String format,Date date) {
        if (null != date) {
            if (StringUtils.isEmpty(format)) {
                format = YYYY_MM_DD_HH_MM_SS;
            }
            String dateStr = parseDateToStr(format, date);
            Date resDate = getDate(format, dateStr);
            return resDate;
        }
        return null;
    }

    /**
     * 计算两个日期相差几天,不计算结束日期当天
     * @param start 开始时间
     * @param end 结束时间
     * @return
     */
    public static long getDiffDays(Date start,Date end) {
        long dayNum = 0;
        if (start != null && end != null) {
            //格式化时间
            Date startdate = getChangeDate(YYYY_MM_DD,start);
            Date enddate = getChangeDate(YYYY_MM_DD,end);
            //获取时间差
            long subtract = enddate.getTime() - startdate.getTime();
            //转换为天数
            dayNum = subtract / 1000 / 60 / 60 / 24;
        }
        return dayNum;
    }

    /**
     * 判断是不是本周
     * @param date
     * @return
     */
    public static boolean isThisWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(date);
        int dateWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (currentWeek == dateWeek) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否是当天
     * @param date
     * @return
     */
    public static boolean isThisDay (Date date) {
        return isThisTime(date,YYYY_MM_DD);
    }

    /**
     * 判断是否是本月
     * @param date
     * @return
     */
    public static boolean isThisMonth (Date date) {
        return isThisTime(date,YYYY_MM);
    }

    /**
     * 判断当前时间
     * @param date
     * @param format
     * @return
     */
    private static boolean isThisTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dateFormat = sdf.format(date);
        String nowFormat = sdf.format(new Date());
        if (dateFormat.equals(nowFormat)) {
            return true;
        }
        return false;
    }


}

注: 可直接使用,如有问题欢迎交流,后续遇到好的会继续更新

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • java 日期格式化工具类
  • 代码如下
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档