public class DateUtil {
//字符串转秒级时间搓
public static Long strToIntTime(String str, String layout) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
try {
Date date = simpleDateFormat.parse(str);
return date.getTime() / 1000;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String dateToString(Date date, String layout) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
return simpleDateFormat.format(date);
}
//获取当天凌晨00:00:00的时间搓
public static Long todayStartTime() {
long nowTime = System.currentTimeMillis();
long todayStartTime = nowTime - (nowTime + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
//dateCursor.setTodayStart((Date)currentDate.getTime().clone());
return todayStartTime;
}
//判断两个日期是否在一个星期内
public static boolean isTheSameWeek(Date date, Date date1) {
int a = getWeekAndYear(date);
int b = getWeekAndYear(date1);
if (a == b) {
return true;
} else {
return false;
}
}
/**
* 判断两个日期是否间隔大于7天
*
* @param date 开始日期 单位:时间戳毫秒
* @param date1 结束日期 单位:时间戳毫秒
* @return true 为大于,false为不大于
*/
public static boolean isGtSevenDay(long date, long date1) {
System.out.println((date1 - date) + "时间间隔");
if (date1 - date > 7 * 86400000) {
System.out.println(date1 - date);
return true;
}
return false;
}
//Date转秒级时间搓
public static Long dateToIntTime(Date date) {
return date.getTime() / 1000;
}
/**
* 判断某日期属于第几个星期
*
* @param d
* @return
*/
public static int getWeekAndYear(Date d) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(d);
int week = cal.get(Calendar.WEEK_OF_YEAR);
return week;
}
/**
* 判断两个时间是否大于一个月
*
* @return true 大于,false不大于
*/
public static Boolean twoTimeMoreThanOneMonth(Long startTime, Long endTime) {
Long resultTime = endTime - startTime;
Long monthTime = 31 * 24 * 60 * 60 * 1000L;
if (resultTime > monthTime) {
return true;
} else {
return false;
}
}
/**
* 判断是否包含今天
*
* @param endTime
* @return
*/
public static Boolean isIncluToday(Long endTime) {
long nowTime = System.currentTimeMillis();
if (endTime > nowTime) {
return true;
} else {
return false;
}
}
/**
* 把Date 换成 String
*
* @param date
* @param layout
* @return
*/
public static String toStr(Date date, String layout) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
String str = simpleDateFormat.format(date);
return str;
}
//把Date 换成 String
public static String newStringTime(String layout) {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
String str = simpleDateFormat.format(date);
return str;
}
public static Date StringToDate(String time, String layout) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
try {
return simpleDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
//把Date 换成 String
public static String newStringTime(Date date, String layout) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
String str = simpleDateFormat.format(date);
return str;
}
/**
* 把毫秒时间戳 换成 String
*
* @param time 时间戳
* @param layout 格式化格式:例如yyyy-MM-dd HH:mm:ss
* @return 根据格式化格式返回对应的时间
*/
public static String getStringTime(long time, String layout) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
String str = simpleDateFormat.format(time);
return str;
}
public static int getStringToIntTime(String time, String layout) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
try {
return (int) (simpleDateFormat.parse(time).getTime() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public static String StringToString(String time, String layout) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
Date date = null;
try {
date = simpleDateFormat.parse(time);
System.out.println(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = simpleDateFormat.format(date);
return str;
}
public static String wxToRrgTime(String time) {
String layout = "yyyyMMddHHmmss";
return StringToString(time, layout);
}
public static String aliToRrgTime(Date time) {
String layout = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(layout);
return simpleDateFormat.format(time);
}
/**
* 获取离当前时间天数的开始时间
* @param gapDate gapDate=0表示今天,gapDate>0表示今天之后,gapDate<0表示今天之前
* @return
*/
public static Date getBeginDayOfGapDate(int gapDate) {
Calendar cal =Calendar.getInstance();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, gapDate);
return cal.getTime();
}
//获取当天的开始时间
private static Date getDayBegin() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* 获取离当前时间天数的结束时间
* @param gapDate gapDate=0表示今天,gapDate>0表示今天之后,gapDate<0表示今天之前
* @return
*/
public static Date getEndDayOfGapDate(int gapDate) {
Calendar cal = Calendar.getInstance();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, gapDate);
return cal.getTime();
}
//获取当天的结束时间
private static Date getDayEnd() {
Calendar cal =Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
/**
* 获取昨日的日期格式串
*
* @return String
*/
public static Date getYesterday() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
return calendar.getTime();
}
/**
* 获取某天的日期格式串
*
* @param amount 昨天为-1 以此类推
* @return String
*/
public static Date getBeforeYesterday(Integer amount) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, amount);
return calendar.getTime();
}
/**
* 通过时间秒毫秒数判断两个时间的间隔
*
* @param date1 开始时间;单位:时间戳毫秒
* @param date2 结束时间;单位:时间戳毫秒
* @return
*/
public static Long differentDays(Long date1, Long date2) {
Long days = (date2 - date1) / (1000 * 3600 * 24);
return days;
}
/**
* 通过时间秒毫秒数判断两个时间毫秒差
*
* @param date1 开始时间;单位:时间戳毫秒
* @param date2 结束时间;单位:时间戳毫秒
* @return
*/
public static Long different(Long date1, Long date2) {
Long days = date2 - date1;
return days;
}
/**
* 获取过去第几天的日期
*
* @param past
* @return
*/
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String result = format.format(today);
return result;
}
public static String respTime() {
return newStringTime("yyyy-MM-dd HH:mm:ss");
}
public static void main(String[] args) {
System.out.println(getBeginDayOfGapDate(0));
}
}