Java常用日期处理工具函数类

本文介绍了一个实用的日期处理工具类,包含日期格式化、解析、增减天数等功能,并提供了获取当天起始和结束时间的方法,支持日期比较及判断是否为当天等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/**
* 常用日期处理工具类
*/
public class dateUtils{

/**
* 格式化当前时间
* @param pattern
* @return
*/
public static String format(String pattern){
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(new Date());
}

/**
* 格式化指定时间
* @param date
* @param pattern
* @return
*/
public static String format(Date date, String pattern){
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}


/**
* 解释日期
* @param str
* @param pattern
* @return
* @throws Exception
*/
public static Date parse(String str, String pattern) throws Exception{
SimpleDateFormat df = new SimpleDateFormat(pattern);
Date date = df.parse(str);
return date;
}

/**
* 得到当天开始日期
* @return
* @throws Exception
*/
public static Date getDate() throws Exception{
return OspUtils.parse(OspUtils.format("yyyyMMdd"),"yyyyMMdd");
}

/**
* 在指定的日期增加天数
* @param initDate
* @param diff
* @return
*/
public static Date addDay2Date(Date initDate , int diff){
Calendar cal = Calendar.getInstance();
cal.setTime(initDate);
cal.add(Calendar.DATE, diff);
return new Date(cal.getTimeInMillis());
}

/**
* 返回当天起始时间
* @return
*/
public static Date getCurrentDayStart() throws Exception{
String start = format("yyyyMMdd")+"000000";
return parse(start, yyyyMMddhhmmss);

}

/**
* 返回当天结束时间
* @return
*/
public static Date getCurrentDayend() throws Exception{
String start = format("yyyyMMdd")+"235959";
return parse(start, yyyyMMddhhmmss);

}

/**
* 判断日期是否是当天时间
* @param date
* @return
*/
public static boolean judgeDateIsCurrentDay(Date date){

try {
long time = date.getTime();
long Daystart = getCurrentDayStart().getTime();
long Dayend = getCurrentDayend().getTime();
return Daystart<=time && time<=Dayend;
} catch (Exception e) {
}
return false;
}

/**
* 比较日期字符串返回天数
* @param strBegin 格式:yyyy-MM-dd
* @param strEnd 格式:yyyy-MM-dd
* @return
* @throws Exception
*/
public static int getDifferDays(String strBegin,String strEnd) throws Exception {

java.util.Calendar calst = java.util.Calendar.getInstance();
java.util.Calendar caled = java.util.Calendar.getInstance();

calst.setTime(parse(strBegin.concat(" 00:00:00"), yyyyMMddhhmmss));
caled.setTime(parse(strEnd.concat(" 00:00:00"), yyyyMMddhhmmss));

//得到两个日期相差的天数
int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst
.getTime().getTime() / 1000)) / 3600 / 24;

return days;

}

/**
* 比较日期返回天数
* @param strBegin
* @param strEnd
* @return
*/
public static final int getDifferDays(Date strBegin, Date strEnd) {
java.util.Calendar calst = java.util.Calendar.getInstance();
java.util.Calendar caled = java.util.Calendar.getInstance();
calst.setTime(strBegin);
caled.setTime(strEnd);
//设置时间为0时
calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
calst.set(java.util.Calendar.MINUTE, 0);
calst.set(java.util.Calendar.SECOND, 0);
caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
caled.set(java.util.Calendar.MINUTE, 0);
caled.set(java.util.Calendar.SECOND, 0);

//得到两个日期相差的天数
int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst
.getTime().getTime() / 1000)) / 3600 / 24;

return days;
}


public static final String DATE_PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
public static final String yyyyMMddhhmmss = "yyyyMMddhhmmss";

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值