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);
}
}
java 日期工具类
最新推荐文章于 2024-10-03 08:33:02 发布
这是一个Java日期工具类,提供日期转时间戳、字符串格式化、日期转换、时间差计算以及创建时间Key等功能。主要方法包括DateToTimeStamp、DateToString、DateStringToDate、getTimedifference和createTimeKey。适用于需要进行日期时间操作的场景。
1950

被折叠的 条评论
为什么被折叠?



