I have to calculate total fly time minutes between departure airport and arrival airport.
This job is done by this code snippet:
public int calculateFlightDuration(String departureDateTime, String depAirportCode, String arrivalDateTime,
String arrAirportCode) {
try {
LocalDateTime depLocalTime = LocalDateTime.parse(departureDateTime, formatter);
LocalDateTime arrLocalTime = LocalDateTime.parse(arrivalDateTime, formatter);
ZoneOffset depZoneOffset = getTimeZoneOffset(depAirportCode);
ZoneOffset arrZoneOffset = getTimeZoneOffset(arrAirportCode);
if (depZoneOffset != null && arrZoneOffset != null) {
OffsetDateTime offsetDepTime = OffsetDateTime.of(depLocalTime, depZoneOffset);
OffsetDateTime offsetArrTime = OffsetDateTime.of(arrLocalTime, arrZoneOffset);
Duration flightDuration = Duration.between(offsetArrTime, offsetDepTime).abs();
return (int) flightDuration.toMinutes();
}
} catch (Exception e) {
LOG.warn("::calculateFlightDuration depTime:{} dep.code:{} arrTime:{} arr.code:{}", departureDateTime,
depAirportCode, arrivalDateTime, arrAirportCode);
}
return 0;
}
Here is the problem:
When I want to calculate future flight's duration with those parameters:
depLocalTime = 2017-11-06T14:50
arrLocalTime = 2017-11-06T16:45
depZoneOffset = +03:00
arrZoneOffset = +02:00
as a result of those parameters, flightDuration object is:
flightDuration = PT2H55M
Seems everything is OK, right? But it's not OK actually. Let me explain;
The departure airport code is IST (Turkey) and arrival airport code is AMS (Netherlands) and here is the key:
After 2017-10-29 (before the time I calculated), AMS time will be backed by 1 hour, and its offset will be +01:00, and the IST offset still remain +03:00. So the correct duration object have to be:
flightDuration = PT3H55M
How can I fix this problem? It's really annoying. Thanks for your help.
Edit after ZonedDateTime comments:
Guys, I also tried with ZonedDateTime object, on calculations. Here is the code with ZonedDateTime object used, it makes no difference on the result.
public int calculateFlightDuration(String departureDateTime, String depAirportCode, String arrivalDateTime,
String arrAirportCode) {
try {
LocalDateTime depLocalTime = LocalDateTime.parse(departureDateTime, formatter);
LocalDateTime arrLocalTime = LocalDateTime.parse(arrivalDateTime, formatter);
ZoneOffset depZoneOffset = getTimeZoneOffset(depAirportCode);
ZoneOffset arrZoneOffset = getTimeZoneOffset(arrAirportCode);
if (depZoneOffset != null && arrZoneOffset != null) {
ZonedDateTime zonedDepTime = ZonedDateTime.of(depLocalTime, depZoneOffset);
ZonedDateTime zonedArrTime = ZonedDateTime.of(arrLocalTime, arrZoneOffset);
// OffsetDateTime offsetDepTime = OffsetDateTime.of(depLocalTime, depZoneOffset);
// OffsetDateTime offsetArrTime = OffsetDateTime.of(arrLocalTime, arrZoneOffset);
Duration flightDuration = Duration.between(zonedDepTime, zonedArrTime).abs();
return (int) flightDuration.toMinutes();
}
} catch (Exception e) {
LOG.warn("::calculateFlightDuration depTime:{} dep.code:{} arrTime:{} arr.code:{}", departureDateTime,
depAirportCode, arrivalDateTime, arrAirportCode);
}
return 0;
}
After the @Joe C 's answer, I changed the code again and I believe that this is the way I should go:
public int calculateFlightDuration(String departureDateTime, String depAirportCode, String arrivalDateTime,
String arrAirportCode) {
try {
LocalDateTime depLocalTime = LocalDateTime.parse(departureDateTime, formatter);
LocalDateTime arrLocalTime = LocalDateTime.parse(arrivalDateTime, formatter);
ZoneId depZoneId = getTimeZoneId(depAirportCode);
ZoneId arrZoneId = getTimeZoneId(arrAirportCode);
if (depZoneId != null && arrZoneId != null) {
ZonedDateTime zonedDepTime = ZonedDateTime.of(depLocalTime, depZoneId);
ZonedDateTime zonedArrTime = ZonedDateTime.of(arrLocalTime, arrZoneId);
Duration flightDuration = Duration.between(zonedDepTime, zonedArrTime).abs();
return (int) flightDuration.toMinutes();
}
} catch (Exception e) {
LOG.warn("::calculateFlightDuration depTime:{} dep.code:{} arrTime:{} arr.code:{}", departureDateTime,
depAirportCode, arrivalDateTime, arrAirportCode);
}
return 0;
}
BUT: Java assumes that Istanbul also change its timezone offset to +02:00, but IT WILL NOT HAPPEN. I think I also need to get my Java updated. Here is the result after code change:
depZoneId = Europe/Istanbul
arrZoneId = Europe/Amsterdam
zonedDepTime = 2017-11-06T14:50+02:00[Europe/Istanbul] //damn it's really annoying!
zonedArrTime = 2017-11-06T16:45+01:00[Europe/Amsterdam]
aaand flight duration remains the same:
flightDuration = PT2H55M
Thanks for the answers guys. Now I have to fix the timezone change of Istanbul.
OffsetDateTime
assumes a common offset for the entire year (ex. UTC+2). It does not cover anything along the lines of Summer Time.
If you want to account for Summer Time, you should use a ZonedDateTime
instead, with a ZoneId
. In the case of Europe/Amsterdam
, it will select either UTC+1 or UTC+2 depending on the time of year.
ZonedDateTime zonedDepTime = ZonedDateTime.of(depLocalTime, ZoneId.of("Asia/Istanbul"));
ZonedDateTime zonedArrTime = ZonedDateTime.of(arrLocalTime, ZoneId.of("Europe/Amsterdam"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With