package date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*@author: youthcool
*@date: 2019/9/6
*@description: 时间工具类
*/
public class DateUtils {
/**
* @param start 开始日期
* @param end 结束如期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 比较两个时间之前相差的天数(忽略小时, 分, 秒的影响)
*/
public static Integer diffDay(Date start, Date end) {
if (start == null || end == null) {
return null;
}
return (int) ((getDate(end).getTimeInMillis() - getDate(start).getTimeInMillis()) / (1000 * 3600 * 24));
}
/**
* @param date 开始日期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 获取日期部分的时间(去除时间部分)
*/
public static Calendar getDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getDate(calendar);
}
/**
* @param str 字符串时间格式--"2019-9-6"
* @param pattern 转成什么格式
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description
*/
public static Date changeStringDateToFormat(String str, String pattern){
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(str);
} catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}
/**
* @param calendar Calendar格式
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 获取日期部分的时间(去除时间部分)
*/
public static Calendar getDate(Calendar calendar) {
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar;
}
/**
* @param date 指定日期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 获取指定时间再本周的索引,周一为0
*/
public static int getIndexOfDate(Date date) {
if (date == null) return -1;
Calendar instance = Calendar.getInstance();
instance.setTime(date);
/**
* 该方法返回的值1-7,周日为1,周六为7,周一为2
*/
int weekIndex = instance.get(Calendar.DAY_OF_WEEK);
if (weekIndex == 1) weekIndex = 8;
return weekIndex - 2;
}
/**
* @param date 指定日期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 返回指定时间所在周的最后一天
*/
public static Date getWeekLastDay(Date date) {
if (date == null) return null;
Calendar instance = Calendar.getInstance();
instance.setTime(date);
/**
* 该方法返回的值1-7,周日为1,周六为7,周一为2
*/
int weekIndex = instance.get(Calendar.DAY_OF_WEEK);
if (weekIndex == 1) weekIndex = 8;
instance.add(Calendar.DAY_OF_YEAR, 8 - weekIndex);
instance.set(Calendar.HOUR_OF_DAY, 23);
instance.set(Calendar.MINUTE, 59);
instance.set(Calendar.SECOND, 59);
instance.set(Calendar.MILLISECOND, 999);
return instance.getTime();
}
/**
* @param date 指定日期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 返回指定时间所在周的第一天
*/
public static Date getWeekFirstDay(Date date) {
if (date == null) return null;
Calendar instance = Calendar.getInstance();
instance.setTime(date);
/**
* 该方法返回的值1-7,周日为1,周六为7,周一为2
*/
int weekIndex = instance.get(Calendar.DAY_OF_WEEK);
if (weekIndex == 1) weekIndex = 8;
instance.add(Calendar.DAY_OF_YEAR, 2 - weekIndex);
instance.set(Calendar.HOUR_OF_DAY, 0);
instance.set(Calendar.MINUTE, 0);
instance.set(Calendar.SECOND, 0);
instance.set(Calendar.MILLISECOND, 0);
return instance.getTime();
}
/**
* @param date 指定日期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 获取指定日期当月的最后一天(2019/9/6->Tue Oct 01 00:00:00 CST 2019)
*/
public static Date getLastDayOfGiven(Date date) {
if (date == null) return null;
return addSeconds(getFirstDayOfNextMonth(date), -1);
}
/**
* @param date 指定日期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 获取指定日期下个月的第一天
*/
public static Date getFirstDayOfNextMonth(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.DATE_FORMAT);
String dateStr = sdf.format(date);
try {
date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.MONTH, 1);
return calendar.getTime();
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* @param startDay 开始日期
* @param endDay 结束日期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 计算两个时间之间相隔天数
*/
public static int getDays(Date startDay, Date endDay) {
if (startDay.after(endDay)) {
Date start = startDay;
startDay = endDay;
endDay = start;
}
long start = startDay.getTime();
long end = endDay.getTime();
long ei = end - start;
return (int) (ei / (1000 * 60 * 60 * 24));
}
/**
* @param date 指定日期
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 获取指定日期当月的第一天
*/
public static Date getFirstDayOfGivenMonth(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.DATE_FORMAT);
String dateStr = sdf.format(date);
try {
date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.MONTH, 0);
return calendar.getTime();
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* @param date 时间
* @param second 几秒钟
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 在指定时间上添加秒
*/
public static Date addSeconds(Date date, int second) {
if (date == null) return null;
Calendar instance = Calendar.getInstance();
instance.setTime(date);
instance.add(Calendar.SECOND, second);
return instance.getTime();
}
/**
* @param date 时间
* @param minute 几分钟
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 在指定时间上添加分钟
*/
public static Date addMinutes(Date date, int minute) {
if (date == null) return null;
Calendar instance = Calendar.getInstance();
instance.setTime(date);
instance.add(Calendar.MINUTE, minute);
return instance.getTime();
}
/**
* @param date 时间
* @param hour 几小时
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 在指定时间上添加小时
*/
public static Date addHours(Date date, int hour) {
if (date == null) return null;
Calendar instance = Calendar.getInstance();
instance.setTime(date);
instance.add(Calendar.HOUR_OF_DAY, hour);
return instance.getTime();
}
/**
* @param date 时间
* @param millisecond 几毫秒
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 在指定时间上添加毫秒
*/
public static Date addMillisecond(Date date, int millisecond) {
if (date == null) return null;
Calendar instance = Calendar.getInstance();
instance.setTime(date);
instance.add(Calendar.MILLISECOND, millisecond);
return instance.getTime();
}
/**
* @return java.util.Date
* @author youthcool
* @date 2019/8/27
* @description 获取上一个月的结束时间
*/
public static Date getEndOfLastMonth(){
SimpleDateFormat simpleDateFormat=new SimpleDateFormat(DateFormat.DATETIME_FORMAT);
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
int lastMonthMaxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
return calendar.getTime();
}
/**
* @param date 时间
* @param dateType 转化类型
* @param month 加几个月
* @return java.util.Date
* @author youthcool
* @date 2019/9/3
* @description 在某个时间上加几个月
*/
public static Date addMonth(Date date, String dateType, int month) {
Date nowDate = null;
SimpleDateFormat format = new SimpleDateFormat(dateType);
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, month);
int lastMonthMaxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
String now = format.format(calendar.getTime());
nowDate=format.parse(now);
} catch (Exception e) {
e.printStackTrace();
}
return nowDate;
}
/**
* @param date 日期
* @param days 天数
* @return java.util.Date
* @author youthcool
* @date 2019/9/6
* @description 在指定时间上添加天数
*/
public static Date addDays(Date date, int days) {
if (date == null) return null;
Calendar instance = Calendar.getInstance();
instance.setTime(date);
instance.add(Calendar.DAY_OF_YEAR, days);
return instance.getTime();
}
/**
* @author youth
* @date 2019/9/4
* @description 日期格式常量接口
*/
public interface DateFormat{
String DATE_FORMAT="yyyy-MM-dd";
String HOUR_MINUTE="hh:mm";
String DATE_FORMAT_EN = "dd/MM/yyyy";
String DATETIME_FORMAT ="yyyy-MM-dd HH:mm:ss.SSS";
String DATE_MOBNTH = "yyyy-MM";
String DATE_DAY = "dd";
String WITHOUT_SECOND_FORMAT="yyyy-MM-dd HH:mm";
String WITHOUT_SECOND_FORMAT_EN="dd/MM/yyyy HH:mm";
String HOUR_OF_DAY_MINUTE="HH:mm";
}
}