计算两个时间的年份差
/**
* 获取两个日期相差的年数
*
* @param d1 较大的日期
* @param d2 较小的日期
* @return 如果d1>d2返回 月数差 否则返回0
*/
public static int yearsBetween(Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
//如果开始时间小于结束时间 则直接返回0
if (c1.getTimeInMillis() < c2.getTimeInMillis()) {
return 0;
}
int surplus = c2.get(Calendar.DATE) - c1.get(Calendar.DATE);
int result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
if(result < 0){
result = 1;
}else if(result == 0){
result = surplus <= 0 ? 1 : 0;
}else{
result = 0;
}
return ((Math.abs(year)) + result);
}
计算两个时间相差的月份
/**
* 获取两个日期相差的月数
*
* @param d1 较大的日期
* @param d2 较小的日期
* @return 如果d1>d2返回 月数差 否则返回0
*/
public static int monthsBetween(Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
if (c1.getTimeInMillis() < c2.getTimeInMillis()) return 0;
int year1 = c1.get(Calendar.YEAR);
int year2 = c2.get(Calendar.YEAR);
int month1 = c1.get(Calendar.MONTH);
int month2 = c2.get(Calendar.MONTH);
int day1 = c1.get(Calendar.DAY_OF_MONTH);
int day2 = c2.get(Calendar.DAY_OF_MONTH);
// 获取年的差值 假设 d1 = 2015-8-16 d2 = 2011-9-30
int yearInterval = year1 - year2;
// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
if (month1 < month2 || month1 == month2 && day1 < day2) yearInterval--;
// 获取月数差值
int monthInterval = (month1 + 12) - month2;
if (day1 < day2) monthInterval--;
monthInterval %= 12;
return yearInterval * 12 + monthInterval;
}
计算两个时间的天数差:
传入两个时间 date1 和 date2 然后将时间进行格式化,将格式化后的时间放入Calendar 对象的settime属性中,用时间戳进行运算,最后得出两个时间相差的天数
/**
* 计算date2 - date1之间相差的天数
*
* @param date1
* @param date2
* @return 如果d1>d2返回 月数差 否则返回0
*/
public static int daysBetween(Date date1, Date date2) {
DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
try {
Date d1 = sdf.parse(DateUtils.dateStr7(date1));
Date d2 = sdf.parse(DateUtils.dateStr7(date2));
cal.setTime(d1);
long time1 = cal.getTimeInMillis();
cal.setTime(d2);
long time2 = cal.getTimeInMillis();
return Integer.parseInt(String.valueOf((time2 - time1) / 86400000L));
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
计算两个时间相差的分钟
/**
* 计算date2 - date1之间相差的分钟
*
* @param date1
* @param date2
* @return
*/
@SuppressWarnings("deprecation")
public static int minutesBetween(Date date1, Date date2) {
Calendar cal = Calendar.getInstance();
// date1.setSeconds(0);
cal.setTime(date1);
long time1 = cal.getTimeInMillis();
cal.setTime(date2);
long time2 = cal.getTimeInMillis();
if (time2 - time1 <= 0) {
return 0;
} else {
return Integer.parseInt(String.valueOf((time2 - time1) / 60000L)) + 1;
}
}
计算两个时间的秒数差
/**
* 计算date2 - date1之间相差的秒
*
* @param date1
* @param date2
* @return
*/
@SuppressWarnings("deprecation")
public static int secondBetween(Date date1, Date date2) {
Calendar cal = Calendar.getInstance();
// date1.setSeconds(0);
cal.setTime(date1);
long time1 = cal.getTimeInMillis();
cal.setTime(date2);
long time2 = cal.getTimeInMillis();
if (time2 - time1 <= 0) {
return 0;
} else {
return Integer.parseInt(String.valueOf((time2 - time1) / 1000L)) + 1;
}
}
计算两个时间的毫秒差
/**
* 计算date2 - date1之间相差的毫秒
*
* @param date1
* @param date2
* @return
*/
@SuppressWarnings("deprecation")
public static int millisecondBetween(Date date1, Date date2) {
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
long time1 = cal.getTimeInMillis();
cal.setTime(date2);
long time2 = cal.getTimeInMillis();
if (time2 - time1 <= 0) {
return 0;
} else {
return Integer.parseInt(String.valueOf((time2 - time1)));
}
}
获取时间前后多少年
/**
* 前/后?年
*
* @param d
* @param year
* @return
*/
public static Date rollYear(Date d, int year) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.YEAR, year);
return cal.getTime();
}
获取时间前后多少月
/**
* 前/后?月
*
* @param d
* @param mon
* @return
*/
public static Date rollMon(Date d, int mon) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.MONTH, mon);
return cal.getTime();
}
获取时间前后多少天
/**
* 前/后?天
*
* @param d
* @param day
* @return
*/
public static Date rollDay(Date d, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.DAY_OF_MONTH, day);
return cal.getTime();
}
获取时间前后多少分钟
/**
* 前/后?分钟
*
* @param d
* @param minute
* @return
*/
public static Date rollMinute(Date d, int minute) {
return new Date(d.getTime() + minute * 60 * 1000);
}
获取指定日期星期几(int)
/**
* 获取指定日期星期几(int)
*
* @param dt
* @return
*/
public static int getWeekOfInt(Date dt) {
int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}
/**
* 获取指定日期星期几
*
* @param dt
* @return
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekDays[w];
}