日期:2014-05-20  浏览次数:20641 次

关于时间的计算
现有2个string变量
a= "08:00 "
b= "5 "
想把他们转换成time相减得到7:55不知该怎么做

------解决方案--------------------
String a= "08:00 ";
String b= "5 ";
SimpleDateFormat formatter = new SimpleDateFormat( "HH:mm ") ;
Date date = formatter.parse(a);
Calendar calendar =Calendar.getInstance() ;
calendar.setTimeInMillis(date.getTime()-Integer.parseInt(b)*60*1000) ;
System.out.println(formatter.format(calendar.getTime()));
------解决方案--------------------
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR,8-c.get(Calendar.HOUR));
c.add(Calendar.MINUTE,0-c.get(Calendar.MINUTE));
c.add(Calendar.MINUTE,-5);
System.out.println(new SimpleDateFormat( "hh:mm ").format(c.getTime()));