-2

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.

1 Answers1

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