获取时间 判断有效期的工具类
程序员文章站
2022-03-03 10:20:11
...
import org.apache.commons.lang.StringUtils;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static final Long IN_EFFECT = 1L;
public static final Long NOT_BEGIN = 2L;
public static final Long EXPIRED = 3L;
private DateUtil() {
}
/**
* 获取当前的默认时间(YYYY-MM-dd)
*
* @return
*/
public static String getNowDefault() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
/**
* 获取某天0点的时间
*/
public static Timestamp getBeginTime(Timestamp time) {
if (time != null) {
String timeStr = new SimpleDateFormat("yyyy-MM-dd").format(time);
return Timestamp.valueOf(timeStr + " 00:00:00.0");
} else {
return null;
}
}
/**
* 获取某天23点59分59秒的时间
*/
public static Timestamp getEndTime(Timestamp time) {
if (time != null) {
String timeStr = new SimpleDateFormat("yyyy-MM-dd").format(time);
return Timestamp.valueOf(timeStr + " 23:59:59.999");
} else {
return null;
}
}
/**
* 判断时间是否在时间段内
* @param nowTime
* @param beginTime
* @param endTime
* @return 1 在时间范围内 2 时间范围后 3 时间范围前
*
*/
public static Long belongCalendar(Date nowTime, Date beginTime, Date endTime) {
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(beginTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return IN_EFFECT;
} else if (date.before(begin)) {
return NOT_BEGIN;
} else {
return EXPIRED;
}
}
/**
* 获取当前时间(yyyyMMdd)
* @return
*/
public static String getCurrentTime() {
return new SimpleDateFormat("yyyyMMdd").format(new Date());
}
/**
* 获取当前时间
* @param patten 时间格式
*/
public static String getCurrentTime(String patten) {
return new SimpleDateFormat(patten).format(new Date());
}
/**
* 将时间装换成字符串
* @param date
* @return 格式yyyy-MM-dd :hh:mm:ss
*/
public static String getDateTimeStr(Date date) {
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
}
/**
* 字符串格式时间转换到对象时间
*
* @param str
* @return
*/
public static Date string2DateTime(String str) {
SimpleDateFormat fo = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
try {
date = fo.parse(str);
} catch (ParseException e) {
//OdyExceptionFactory.log(e);
}
return date;
}
public static Date dayEnd(String str){
if(StringUtils.isBlank(str)){
return null;
}
return dayEnd(string2DateTime(str));
}
public static Date dayStart(String str){
if(StringUtils.isBlank(str)){
return null;
}
return dayStart(string2DateTime(str));
}
public static String dayEndStr(String str){
if(StringUtils.isBlank(str)){
return null;
}
return dayEndStr(string2DateTime(str));
}
public static String dayStartStr(String str){
if(StringUtils.isBlank(str)){
return null;
}
return dayStartStr(string2DateTime(str));
}
/**
* 获取指定时间的那天 23:59:59.999 的时间
*
* @param date
* @return
*/
public static Date dayEnd(Date date) {
if(date==null){
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
/**
* 获取指定时间的那天 00:00:000 的时间
*
* @param date
* @return
*/
public static Date dayStart(Date date) {
if(date==null){
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public static String dayEndStr(Date date) {
if(date==null){
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(c.getTime());
}
public static String dayStartStr(Date date) {
if(date==null){
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(c.getTime());
}
}
上一篇: Neo图数据库与python交互
下一篇: R语言相关性绘图