public static void main(String[] args) {
int sYear = 2016;
//月份是0-11,换算时+1
int sMonth = 9;
int sDay = 10;
int eYear = 2017;
int eMonth = 5;
int eDay = 15;
Calendar sDate = Calendar.getInstance();
sDate.set(sYear, sMonth, sDay);
Calendar eDate = Calendar.getInstance();
eDate.set(eYear, eMonth, eDay);
/*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String sDateStr = "2016-09-10";
String eDateStr = "2017-05-15";
Calendar sDate = Calendar.getInstance();
Calendar eDate = Calendar.getInstance();
try {
sDate.setTime(format.parse(sDateStr));
eDate.setTime(format.parse(eDateStr));
} catch (ParseException e1) {
e1.printStackTrace();
}*/
//一天的毫秒数
long dayMill = 1000*60*60*24;
//一小时的毫秒数
long hourMill = 1000*60*60;
//一分钟的毫秒数
long minMill = 1000*60;
try {
System.out.println("相差年:"+differYear(sDate, eDate));
System.out.println("相差月:"+differMonth(sDate, eDate));
System.out.println("相差日:"+differDay(sDate, eDate, dayMill));
System.out.println("相差周:"+differDay(sDate, eDate, dayMill)/7);
System.out.println("相差小时:"+differDay(sDate, eDate, hourMill));
System.out.println("相差分钟:"+differDay(sDate, eDate, minMill));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取两日期相差年数
* @param sDate 开始日期
* @param eDate 结束日期
* @return
* @throws Exception
*/
public static int differYear(Calendar sDate,Calendar eDate) throws Exception{
try {
return eDate.get(Calendar.YEAR) - sDate.get(Calendar.YEAR);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 获取两日期相差月数
* @param sDate 开始日期
* @param eDate 结束日期
* @return
* @throws Exception
*/
public static int differMonth(Calendar sDate,Calendar eDate) throws Exception{
try {
int eMonth = eDate.get(Calendar.YEAR)*12+eDate.get(Calendar.MONTH);
int sMonth = sDate.get(Calendar.YEAR)*12+sDate.get(Calendar.MONTH);
return eMonth - sMonth;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 获取两日期相差天数、小时、分钟、秒(将毫秒值相减并换算)
* @param sDate 开始日期
* @param eDate 结束日期
* @param differMill 毫秒换算值
* @return
* @throws Exception
*/
public static int differDay(Calendar sDate,Calendar eDate,Long differMill) throws Exception{
try {
int differDay = (int)((eDate.getTimeInMillis()-sDate.getTimeInMillis())/differMill);
return differDay;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}empty
int sYear = 2016;
//月份是0-11,换算时+1
int sMonth = 9;
int sDay = 10;
int eYear = 2017;
int eMonth = 5;
int eDay = 15;
Calendar sDate = Calendar.getInstance();
sDate.set(sYear, sMonth, sDay);
Calendar eDate = Calendar.getInstance();
eDate.set(eYear, eMonth, eDay);
/*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String sDateStr = "2016-09-10";
String eDateStr = "2017-05-15";
Calendar sDate = Calendar.getInstance();
Calendar eDate = Calendar.getInstance();
try {
sDate.setTime(format.parse(sDateStr));
eDate.setTime(format.parse(eDateStr));
} catch (ParseException e1) {
e1.printStackTrace();
}*/
//一天的毫秒数
long dayMill = 1000*60*60*24;
//一小时的毫秒数
long hourMill = 1000*60*60;
//一分钟的毫秒数
long minMill = 1000*60;
try {
System.out.println("相差年:"+differYear(sDate, eDate));
System.out.println("相差月:"+differMonth(sDate, eDate));
System.out.println("相差日:"+differDay(sDate, eDate, dayMill));
System.out.println("相差周:"+differDay(sDate, eDate, dayMill)/7);
System.out.println("相差小时:"+differDay(sDate, eDate, hourMill));
System.out.println("相差分钟:"+differDay(sDate, eDate, minMill));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取两日期相差年数
* @param sDate 开始日期
* @param eDate 结束日期
* @return
* @throws Exception
*/
public static int differYear(Calendar sDate,Calendar eDate) throws Exception{
try {
return eDate.get(Calendar.YEAR) - sDate.get(Calendar.YEAR);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 获取两日期相差月数
* @param sDate 开始日期
* @param eDate 结束日期
* @return
* @throws Exception
*/
public static int differMonth(Calendar sDate,Calendar eDate) throws Exception{
try {
int eMonth = eDate.get(Calendar.YEAR)*12+eDate.get(Calendar.MONTH);
int sMonth = sDate.get(Calendar.YEAR)*12+sDate.get(Calendar.MONTH);
return eMonth - sMonth;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 获取两日期相差天数、小时、分钟、秒(将毫秒值相减并换算)
* @param sDate 开始日期
* @param eDate 结束日期
* @param differMill 毫秒换算值
* @return
* @throws Exception
*/
public static int differDay(Calendar sDate,Calendar eDate,Long differMill) throws Exception{
try {
int differDay = (int)((eDate.getTimeInMillis()-sDate.getTimeInMillis())/differMill);
return differDay;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}