当前位置: 代码迷 >> J2EE >> 对“2015-10-15 08:十:00”时间做差值运算
  详细解决方案

对“2015-10-15 08:十:00”时间做差值运算

热度:814   发布时间:2016-04-17 22:59:47.0
对“2015-10-15 08:10:00”时间做差值运算
求助,用Java代码怎么求出“2015-10-15 08:10:00”与“2015-10-16 16:00:00”的时间差呢?
这里面包含对月、日、时分秒的差值运算,求大神帮忙
------解决思路----------------------

/**
 * 按两个日期型字符串,计算两个日期相隔的天数
 * 
 * @param firstString
 * @param secondString
 * @return int 天数
 */
public static String nDays(String firstString, String secondString)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date firstDate = null;
Date secondDate = null;
try
{
firstDate = df.parse(firstString);
secondDate = df.parse(secondString);
} catch (Exception e)
{
// 日期型字符串格式错误
}

long time=(secondDate.getTime() - firstDate.getTime())/1000;
int nDay = (int) ( time/(24*60*60) );
int nHour = (int) ( (time - nDay*24*60*60)/(60*60) );
int nMinute = (int) ( (time - nDay*24*60*60-nHour*60*60)/60 );
int nSecond = (int) ( time - nDay*24*60*60-nHour*60*60-nMinute*60 );

String message=nDay + "天" + nHour + "小时" + nMinute + "分" + nSecond + "秒";
System.out.println(message);
return message;
}
  相关解决方案