在我的申请中,我从JSON到达时间和出发时间分别为字符串2014-12-15T23:00:00和2014-12-15T13:30:00。现在我需要知道字符串中的小时数以及出发时间和到达时间之间的差异。
答案 0 :(得分:0)
试试此代码
String dtDeparture = "2014-12-15T13:30:00";
String dtArrival = "2014-12-15T23:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try {
Date dateDeparture = format.parse(dtDeparture);
Date dateArrival = format.parse(dtArrival);
dateArrival.compareTo(dateDeparture);
long diff = dateArrival.getTime() - dateDeparture.getTime();
long hours = TimeUnit.MILLISECONDS.toHours(diff);
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff) - hours*60;
System.out.println(hours + " hours and " + minutes + " minutes");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}