0

Lets say I have to subtract Arrival time by departing time of specific days. How would I do this in Java using Gregorian calendar?

public Duration getDuration(){
  SimpleDateFormat date = new SimpleDateFormat("YYYYMMDD");
  SimpleDateFormat time = new SimpleDateFormat("HHMM");

  String ArivalTime = "1720"
  String DepartingTime = "1100"
  String ArivalDate = "20200220"
  String DepartingDate = "20200211"

  Calendar departureDateCalc = new GregorianCalendar();


  //return duration;
}

the code is barely anything. But I need to subtract an Arrival date by Departing date

HoRn
  • 1,458
  • 5
  • 20
  • 25
  • 2
    Use [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html). It has everything you need. – akuzminykh Feb 08 '20 at 11:44
  • 2
    See https://stackoverflow.com/questions/27005861/calculate-days-between-two-dates-in-java-8 – Joakim Danielson Feb 08 '20 at 12:01
  • look at this https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances – HaMi Feb 08 '20 at 12:01
  • I recommend you don’t use `SimpleDateFormat` and `GregorianCalendar`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate`, `LocalTime`, `DateTimeFormatter` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 08 '20 at 12:11
  • For starters format pattern letters are case sensitive. Please check once more whether you need uppercase `YYYY` or lowercase `yyyy`, etc. – Ole V.V. Feb 08 '20 at 12:12
  • In which time zone is the departure time? And in which time zone the arrival time? – Ole V.V. Feb 08 '20 at 12:19

1 Answers1

3

java.time

Don’t use GregorianCalendar for this. Use java.time, the modern Java date and time API.

    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmm");

    String arrivalTime = "1720";
    String departingTime = "1100";
    String arrivalDate = "20200220";
    String departingDate = "20200211";
    String arrivalTimeZone = "Africa/Niamey";
    String departingTimeZone = "America/Mendoza";

    ZonedDateTime departure = LocalDate.parse(departingDate, DateTimeFormatter.BASIC_ISO_DATE)
            .atTime(LocalTime.parse(departingTime, timeFormatter))
            .atZone(ZoneId.of(departingTimeZone));
    ZonedDateTime arrival = LocalDate.parse(arrivalDate, DateTimeFormatter.BASIC_ISO_DATE)
            .atTime(LocalTime.parse(arrivalTime, timeFormatter))
            .atZone(ZoneId.of(arrivalTimeZone));

    Duration difference = Duration.between(departure, arrival);
    System.out.println(difference);

This outputs:

PT218H20M

So a period of time of 218 hours 20 minutes. If you want it more readable:

    String diffString = String.format(
            "%d days %d hours %d minutes", difference.toDays(),
            difference.toHoursPart(), difference.toMinutesPart());
    System.out.println(diffString);

9 days 2 hours 20 minutes

The conversion to days assumes that a day is always 24 hours, which may not be the case in either of the departure or the arrival time zone, for example when they go from standard to summer time (DST) or vice versa.

Don’t use GregorianCalendar

The GregorianCalendar class is poorly designed and long outdated. Don’t use it. For anything. If you had wanted to use it for finding a duration, the way would have been to add one day at a time to the departure time until you reach the arrival time. If you’re past it, subtract one day again and start counting hours, again by adding one at a time. Same with minutes. It’s way more complicated and also more error-prone than using the Duration class directly.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161