基本算法:
得到两个时间之间的所差时钟数目(MillionSecond ,nanoseconds),然后根据需要输出的结果
计算并格式化数据.
Example:
import java.util.*;
import java.text.*;
public class Timecalc2 {
public static long MS_SECOND = 1000L;
public static long MS_MINUTE = 60L * MS_SECOND;
public static long MS_HOUR = 60L * MS_MINUTE;
public static long DAY = 24L;
public static long parseDT(String s1, String s2) throws ParseException
{
DateFormat formatter = new SimpleDateFormat("MM.dd.yy HHmm");
Date d1 = formatter.parse(s1);
Date d2 = formatter.parse(s2);
long dateDiff = d2.getTime() - d1.getTime();
return dateDiff;
}
public static String formatMilli(long value)
{
//DecimalFormat is used to display at least two digits
DecimalFormat nf = new DecimalFormat( "00" );
//calculate hours, minutes
long remainder = 0;
long hours = (value / MS_HOUR);
remainder = value % MS_HOUR;
long minutes = remainder / MS_MINUTE;
//build "hh:mm:ss"
StringBuffer buffer = new StringBuffer();
buffer.append(nf.format(hours));
buffer.append( ":" );
buffer.append(nf.format(minutes));
return buffer.toString();
}
public static void main(String[] args)
{
String start = "01.14.03 1730";
String end = "01.17.03 1927";
try
{
long dateDiff = parseDT(start, end);
String answer = formatMilli(dateDiff);
System.out.println(answer);
} catch (ParseException pe)
{
System.out.println("You must use the format: MM.dd.yy HHmm");
}
}
}