-5

I really need your help, I am working on a project for an airport. I need to calculate the duration of the longest flight(in minutes). What I do have is Departure time and arrival time which are both in String. I have not done anything yet because I am clueless as to what I must do.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    First: convert those departure and arrival times into a more suitable type. Which version of Java are you using? Your urgency is irrelevant to the question, by the way... – Jon Skeet May 20 '15 at 17:37
  • 1
    If you want us to help you, you should give us at least two example times. – stevecross May 20 '15 at 17:39
  • Note that in http://stackoverflow.com/questions/4927856/how-to-calculate-time-difference-in-java the answers are (implicitly) considering that both dates are in the same timezone. – Ramón Gil Moreno May 20 '15 at 17:51

1 Answers1

1

You shall use the class java.text.SimpleDateFormat to parse your string into a java.util.Date object. With the method:

public Date parse(String source) throws ParseException

At the creation of the format, you will specify the pattern your time/date text has. FYI, it will be important to consider the timezone. If your input strings contains it: perfect. If not, be sure to take it into account.

Once you have your Date object, extract its time with the getTime() method. It will return a long value with the milliseconds from 1970 in GMT timezone.

If you get this long value for both your departure and arrival time, the difference will tell you the number of milliseconds of the trip.