- 记录笔记
@Slf4j
public class DateUtil {
/**
* 计算日期天数的差
* @param dateStr1 开始日期
* @param dateStr2 结束日期
* @return
* @throws ParseException
*/
public static int daysOfTwo(String dateStr1,String dateStr2) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//跨年不会出现问题
//如果时间为:2016-03-18 11:59:59 和 2016-03-19 00:00:01的话差值为 0
Date fDate= null;
Date oDate= null;
try {
fDate = sdf.parse(dateStr1);
oDate=sdf.parse(dateStr2);
} catch (ParseException e) {
log.error("subtraction date error {}",e);
return -1;
}
long days=(oDate.getTime()-fDate.getTime())/(1000*3600*24);
return (int)days;
}
/**
* 获取下一天的日期
* @param dateStr
* @return
*/
public static String getNextDay(String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
log.error("get next date error {}",e);
return null;
}
calendar.setTime(date);
int day = calendar.get(Calendar.DATE);
// 此处修改为+1则是获取后一天
calendar.set(Calendar.DATE, day + 1);
String nextDay = sdf.format(calendar.getTime());
return nextDay;
}
/**
* Sat Nov 25 2017 00:00:00 GMT+0800 (中国标准时间) 转date
* @return
*/
public static Date cnToDate(String dateString){
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
try{
Date date = sdf.parse(dateString); //将字符串改为date的格式
return date;
}catch(Exception e){
log.error("parse date error {}",e);
return null;
}
}
/**
* Sat Nov 25 2017 00:00:00 GMT+0800 (中国标准时间) 转 date to string 格式 yyyy-MM-dd
* @param dateString
* @return
*/
public static String cnToDateString (String dateString){
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
try{
Date date = sdf.parse(dateString); //将字符串改为date的格式
String resDate= new SimpleDateFormat("yyyy-MM-dd").format(date);
return resDate;
}catch(Exception e){
log.error("parse date error {}",e);
return null;
}
}
/**
* 时间格式
* @param time
* @return
*/
public static Date timeFormat(String time){
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date date = null;
try {
date = format.parse(time);
} catch (ParseException e) {
log.error("parse time error {}",e);
return null;
}
return date;
}
/**
* 将2018-08-15T16:00:00.000Z 时间格式转换成Date类型格式
* @param date 字符串
* @return
*/
public static Date getDateFormat(String date){
try {
date = date.replace("Z", " UTC");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
Date d = null;
d = format.parse(date);
return d;
}catch (Exception e){
log.error("parse date error { }",e);
return null;
}
}
/**
* 将2018-08-15T16:00:00.000Z 时间格式转换成string 类型 yyyy-MM-dd格式
* @param date
* @return
*/
public static String getDateFormatString(String date){
try {
date = date.replace("Z", " UTC");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
Date d = null;
d = format.parse(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatStr = sdf.format(d);
return formatStr;
}catch (Exception e){
log.error("parse date error { }",e);
return null;
}
}
/**
* 获取本周的星期一
* @param date
* @return
*/
public static Date getThisWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
/**
* 获取下周星期1
* @param date
* @return
*/
public static Date getNextWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
/**
* 获取本周的周日
* @param date
* @return
*/
public static Date getThisWeekSunDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 6);
return cal.getTime();
}
// Java获取两个日期之间的所有日期集合
// 1.返回Date的list
private List<Date> getBetweenDates(Date start, Date end) {
List<Date> result = new ArrayList<Date>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
tempStart.add(Calendar.DAY_OF_YEAR, 1);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
while (tempStart.before(tempEnd)) {
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
return result;
}
2.返回yyyy-MM-dd的list
public static List<String> getDays(String startTime, String endTime) {
// 返回的日期集合
List<String> days = new ArrayList<String>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
}