java自定义工具类编写
得到一天的最后时刻
public static Date getDateOneDayEnd(Date date) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
Date date1 = calendar.getTime();
return date1;
}
得到一天的开始时刻
public static Date getOneDatefirst(Date date) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date1 = calendar.getTime();
return date1;
}
得到明天
public static Date addOnyDay(Date date) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
Date date1 = calendar.getTime();
return date1;
}
得到昨天
public static Date reduceOnyDay(Date date) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1);
Date date1 = calendar.getTime();
return date1;
}
得到两个时间的间隔天数
public static long betweenTwoDays(Date date1, Date date2) {
long a1 = date1.getTime();
long a2 = date2.getTime();
long a3 = a2 - a1;
a3 = a3 / (3600 * 24 * 1000);
return a3;
}
解析日期字符串
public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss");
/**
* @param: String
* @return: java.util.Date
*/
public static Date parseDateTime(String dateTime) {
try {
return DATE_TIME_FORMAT.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
用java8封装的新类实现
/**
*
* @param localDateTime
* @