java 日期工具类

这是一个Java日期工具类,提供日期转时间戳、字符串格式化、日期转换、时间差计算以及创建时间Key等功能。主要方法包括DateToTimeStamp、DateToString、DateStringToDate、getTimedifference和createTimeKey。适用于需要进行日期时间操作的场景。
package org.java.demo.ToolPublic;

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

/**
 * 日期工具类
 */
public class DateUtil {

    private String strFormat = "yyyy-MM-dd HH:mm:ss:SSS";
    private final SimpleDateFormat format = new SimpleDateFormat(strFormat);

    public DateUtil() { 
    }

    public SimpleDateFormat getFormat() {
        return format;
    }

    //格式
    private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
    private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
    private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
    private final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");
    private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat sdfTimeMillis = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private final static SimpleDateFormat sdfTimeMilli = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    //*【函数功能】: 将 Date 日期转为 long型时间戳                                                     
    /**************************************************************************************
    * 参数说明: 
    * 		@param date  - Date类型日期       	    
    *返回值说明: long型时间戳
    **************************************************************************************/
    public long DateToTimeStamp(Date date){
		if(date == null){
			return 0;
		}
		return date.getTime();
	}

    //*【函数功能】: 将 Date 日期转为 String 类型                                                      
    /**************************************************************************************
    * 参数说明: 
    * 		@param string  - Date类型日期       	    
    *返回值说明: 返回字符串格式日期 yyyy-MM-dd HH:mm:ss
    **************************************************************************************/
    public String DateToString(Date date){
		if(date == null){
			return "";
		}
		return new SimpleDateFormat(strFormat).format(date);
	}

    //*【函数功能】: 将 String 日期转为 Date 类型                                                      
    /**************************************************************************************
    * 参数说明:  2022-07-13 10:07:01
    * 		@param dateText  - Date类型日期       	    
    *返回值说明: 返回 Date 类型的日期 
    **************************************************************************************/
    public Date StringToDate(String dateText)  {
        try {
            return new SimpleDateFormat(strFormat).parse(dateText);
        } catch (ParseException e) {
            return null;
        }
    }

    //*【函数功能】: 计算日期时间差                                                     
    /**************************************************************************************
    * 参数说明: 
    * 		@param type         - 返回时间差类型  小时: hour  分钟: minute  秒: second  毫秒: millisecond
    *       @param beginTime    - 开始时间
    *       @param endTime      - 结束时间
    *返回值说明: 返回时间差
    **************************************************************************************/
    public long getTimedifference (String type ,Date beginTime, Date endTime){
        if(beginTime==null || endTime==null){
            return 0;
        }

        long diff = endTime.getTime() - beginTime.getTime()  ;
        long reTime = 0;
        switch(type){
            case "hour":
                reTime = diff/1000/60/60;
                break;
            case "minute":
                reTime = diff/1000/60;
                break;
            case "second":
                reTime = diff/1000;
                break;
            case "millisecond":
                reTime = diff;
                break;
            default :
                return 0;
        }
        return reTime;
    }

    //*【函数功能】: 生成时间Key                                                     
    /**************************************************************************************
    * 参数说明: 
    * 		@param   
    *返回值说明: 时间Key
    **************************************************************************************/
    public String createTimeKey(){
        String format = "yyyyMMddHHmmssSSS";
        Date date = new Date();
        return new SimpleDateFormat(format).format(date);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值