日期:2014-05-17 浏览次数:20589 次
SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间格式 Date nowTime=new Date();//获取当前时间 String tmf= matter1.format(nowTime); Date now=matter1.parse(tmf); Date end=matter1.parse(atime);//atime是数据库中事前存的时间 long cha=(now.getTime()-end.getTime())/ (1000 * 60 * 60 * 24);//计算时间差 System.out.println("一号时间差事:"+cha);
------解决方案--------------------
可以将时间存的时候存成long型,算时间差就简单多了,这是存放时间值的方法
public static long getLongTime()
{
try
{
long l = 0L;
Calendar calendar = Calendar.getInstance();
String s = String.valueOf(calendar.get(1));
String s1 = String.valueOf(calendar.get(2) + 1);
String s2 = String.valueOf(calendar.get(5));
String s3 = String.valueOf(calendar.get(10));
String s4 = String.valueOf(calendar.get(9));
String s5 = String.valueOf(calendar.get(12));
String s6 = String.valueOf(calendar.get(13));
if(s4.equals("1"))
s3 = String.valueOf(Long.parseLong(s3) + 12L);
if(s1.length() < 2)
s1 = "0" + s1;
if(s2.length() < 2)
s2 = "0" + s2;
if(s3.length() < 2)
s3 = "0" + s3;
if(s5.length() < 2)
s5 = "0" + s5;
if(s6.length() < 2)
s6 = "0" + s6;
l = Long.parseLong(s + s1 + s2 + s3 + s5 + s6);
return l;
}
catch(Exception exception)
{
return 0L;
}
}
------解决方案--------------------
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date now = df.parse("2008-03-26 13:31:40");
java.util.Date date=df.parse("2008-01-02 11:30:24");
long l=now.getTime()-date.getTime();
long day=l/(24*60*60*1000);
long hour=(l/(60*60*1000)-day*24);
long min=((l/(60*1000))-day*24*60-hour*60);
long s=(l/1000-day*24*60*60-hour*60*60-min*60);
System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");