我想找出当前系统时间和插入的数据&时间之间的差异String
。我试过这个:
try {
String strFileDate = "2012-04-19 15:15:00";
DateFormat formatter2 = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = formatter2.parse(strFileDate);
long difference = date.getTime() - System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(difference);
Toast.makeText(getBaseContext(),
formatter2.format(calendar.getTime()), Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但它并没有给我正确的结果。我做对了吗?
更新
我尝试关注::
String strFileDate = "2012-04-19 15:15:00";
DateFormat formatter2 = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = formatter2.parse(strFileDate);
long diffInMs = date.getTime()
- new Date(System.currentTimeMillis()).getTime();
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);
long hour = diffInSec / (1000 * 60 * 60);
double minutes = diffInSec / (1000 * 60);
// long diffInHour = TimeUnit.to(diffInMs);
Toast.makeText(getBaseContext(),
"left time is :: " + hour + ":" + minutes,
Toast.LENGTH_LONG).show();
输出:: 剩余时间是:: -2 : -131.0
更新(20/4/2012)
try {
Date dt = new Date();
String strFileDate = "2012-04-20 13:10:00";
DateFormat formatter2 = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = formatter2.parse(strFileDate);
String s = getTimeDiff(dt, date);
Log.i("Date is :: >>> ", s);
} catch (Exception e) {
e.printStackTrace();
}
public String getTimeDiff(Date dateOne, Date dateTwo) {
String diff = "";
long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
diff = String.format("%d hour(s) %d min(s)", TimeUnit.MILLISECONDS.toHours(timeDiff),
TimeUnit.MILLISECONDS.toMinutes(timeDiff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeDiff)));
return diff;
}
输出:: 04-20 12:48:06.629: INFO/Date is :: >>>(1295): 2183 hours(s) 38 min(s)