JAVA 时间 日期 相关 工具类

JAVA 时间 日期 相关 工具类

仅用于记录附源码如下

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

/**
 * @Classname DateUtils
 * @Description 时间工具类
 * @Date 2022/5/20 14:51
 */
public class DateUtil {

    /**
     * 日期 转换成 字符串
     *
     * @param date
     * @return string yyyy-MM-dd HH:mm:ss
     */
    public static String DateToString(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeStr = format.format(date);
        return timeStr;
    }

    /**
     * 日期转 换成 字符串
     *
     * @param date
     * @return string yyyy-MM-dd
     */
    public static String DateToString2(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = format.format(date);
        return timeStr;
    }

    /**
     * 日期 转换 字符串
     *
     * @param date
     * @return string yyyy-MM
     */
    public static String DateToString3(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String timeStr = format.format(date);
        return timeStr;
    }

    /**
     * 日期 转换 字符串
     *
     * @param date
     * @return string yyyyMM
     */
    public static String DateToString4(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = format.format(date);
        String timeStr = dateString.replace("-", "").substring(2, 8);
        return timeStr;
    }

    /**
     * 字符串(yyyy-MM-dd HH:mm:ss) 转换 日期
     *
     * @param string
     * @return date
     */
    public static Date StringToDate(String string) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;

        try {
            date = format.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    /**
     * 字符串(yyyy-MM-dd) 转换 日期
     *
     * @param string
     * @return date
     */
    public static Date StringToDate2(String string) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date date = null;
        date = format.parse(string, pos);
        return date;
    }

    /**
     * 获取 一个时间 几天后的时间
     *
     * @param date 索引时间
     * @param days 变化天数
     * @return Date
     */
    public static Date GetDayAfterDays(Date date, int days) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + days);
        return now.getTime();
    }

    /**
     * 获取 一个时间 几月后的时间
     *
     * @param date   索引时间
     * @param mouths 变化月数
     * @return Date
     */
    public static Date GetMouthAfterMouths(Date date, int mouths) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.MONTH, mouths);
        return now.getTime();
    }

    /**
     * 获取 当前日期 是星期几
     *
     * @param pTime 需要判断的时间 yyyy-MM-dd
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */
    public static int GetDayForWeek(String pTime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(pTime));
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    /**
     * 获取两个时间的时间差  毫秒
     *
     * @param time1 (yyyy-MM-dd HH:mm:ss)
     * @param time2 (yyyy-MM-dd HH:mm:ss)
     * @return int
     */
    public static int GetDiffTime(String time1, String time2) throws ParseException {
        //1、指定两个Date对象,业务中可以以参数的形式传递,不用new当前时间。
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //前一个时间
        Date date1 = dateFormat.parse(time1);
        //后一个时间
        Date date2 = dateFormat.parse(time2);

        //2、时间戳相减
        long date1Time = date1.getTime();//前的时间戳
        long date2Time = date2.getTime();//后的时间戳
        int result = (int) (date2Time - date1Time);//毫秒

        return result;
    }

    /**
     * 获取两个时间的时间差  天数
     *
     * @param time1 (yyyy-MM-dd)
     * @param time2 (yyyy-MM-dd)
     * @return int
     */
    public static int GetDiffDays(String time1, String time2) throws ParseException {
        //1、指定两个Date对象,业务中可以以参数的形式传递,不用new当前时间。
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //前一个时间
        Date date1 = dateFormat.parse(time1);
        //后一个时间
        Date date2 = dateFormat.parse(time2);

        //2、时间戳相减
        long date1Time = date1.getTime();//前的时间戳
        long date2Time = date2.getTime();//后的时间戳

        int result = Math.abs((int) (date2Time - date1Time) / (1000 * 60 * 60 * 24));//天数

        return result;
    }

    /**
     * 设置  时间 加时分秒(23:59:59)
     *
     * @param time
     * @return Date
     */
    public static Date MakeDateAll(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);   //设置时间
        calendar.add(Calendar.HOUR, 23);   //日期小时加23小时
        calendar.add(Calendar.MINUTE, 59); //日期分钟加59分钟
        calendar.add(Calendar.SECOND, 59); //日期分钟加59分钟
        Date date = calendar.getTime(); //结果
        return date;
    }

    /**
     * 比较这个时间是否过去
     * (被比较日期大于等于今天返回true)
     * @param  time
     * @return Boolean
     */
    public static Boolean IsAlready(String time) {
        //当前时间
        String date = DateToString2(new Date());
        Integer timeInt = Integer.parseInt(date.replaceAll("-", ""));
        //被比较时间
        Integer timeInt1 = Integer.parseInt(time.replaceAll("-", ""));
        if (timeInt <= timeInt1) {
            //还未发生
            return true;
        } else {
            //已经发生
            return false;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值