In my application i got arrival time and departure time from JSON in the form String 2014-12-15T23:00:00 and 2014-12-15T13:30:00 respectively. Now i need know the hours in the string and difference between the departure time and arrival time.
Asked
Active
Viewed 2,431 times
-2
-
Search on `Google` for that there are many answers available. – M D Dec 12 '14 at 11:37
-
1Show code. What did you try? – MysticMagicϡ Dec 12 '14 at 11:37
-
Always put your code which have you tried – Mahi Dec 12 '14 at 11:54
1 Answers
0
Try this code
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();
}

Mongi Zaidi
- 1,043
- 10
- 15