xBebz
xBebz

Reputation: 1

What is the simplest way to calculate the difference of two time stamps in java?

I have a table (array) that has the current time and the departure time for individual people.

Current Time = CT and Departure Time = DT

Person 1 - CT: 12:00 PM | DT: 2:00 PM Person 2 - CT: 9:30 AM | DT: 10:30 AM Person 3 - CT: 1:00 PM | DT: 2:15 PM Person 4 - CT: 8:00 AM | DT: 3:45 PM

I want to calculate the difference between each time in minutes.


After I know how to calculate the difference between two times in minutes, I have to figure how to calculate the layover time. Meaning, I have to figure out the time period between flights.

Departure Time = DT and Arrival Time = AT Person 5 is very busy today and has to do a few different things in different places.

DT: 11:30 AM | AT: 12:15 PM (LAYOVER TIME PERIOD) DT: 1:15 PM | AT: 3:45 PM (LAYOVER TIME PERIOD) DT: 4:00 PM | AT: 6:45 PM (LAYOVER TIME PERIOD) DT: 10:15 PM | AT: 11:00 PM

I need to find the layover time amount between each flight. How would I go about do this?

Upvotes: 0

Views: 1449

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533500

if you use GMT everywhere your calculations become much simpler. You select a timezone only when printing to a user.

long dateTime1 = ... // something in milli-seconds
long dateTime2 = ... // something in milli-seconds.

long diff = dateTime2 - dateTime1;

long diffInMins = (dateTime2 - dateTime1) / 60000; // to round down.
long diffInMins = (dateTime2 - dateTime1 + 30000) / 60000; // to round half up.
long diffInMins = (dateTime2 - dateTime1 + 59999) / 60000; // to round up.

Technically computers don't support UTC with leap seconds, they only support GMT which doesn't have leap seconds.

Upvotes: 1

Basil Bourque
Basil Bourque

Reputation: 338516

Unclear Question

Your question is unclear. See my comment regarding what seems to be two or three different problems. Also, you omitted any mention of dates and time zones both of which are critical if you want to account for Daylight Saving Time and other anomalies. Nevertheless, I'll give a bit of an answer.

Joda-Time

Use Joda-Time 2.3, the third-party library meant to replace the notoriously bad java.util.Date/Calendar classes.

Time Spans

Read about the three types of time spans in Joda-Time in this question: Joda-Time: what's the difference between Period, Interval and Duration?

Pairs of Instants

Your three problems seem to involve pairs of points along the timeline of the universe:

  • Current Time and Departure Time (for each person)
  • Departure Time and Arrival Time (for each flight)
  • Arrive Time and Departure Time (for each layover)

Representing a span of time defined by a pair of instants along the time-line happens to be the very purpose of the Interval class.

Minutes

If you want minutes, use the Minutes class.

Example Code

Some example code in Joda-Time 2.3 on Java 7.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;

DateTimeZone denverTimeZone = DateTimeZone.forID( "America/Denver" );

// Person 1 - CT: 12:00 PM | DT: 2:00 PM
DateTime person1_start = new DateTime( 2013, DateTimeConstants.NOVEMBER, 3, 12, 00, denverTimeZone ); // year, month, day, hour, minutes
DateTime person1_stop = new DateTime( 2013, DateTimeConstants.NOVEMBER, 3, 14, 00, denverTimeZone );
Interval person1_interval = new Interval( person1_start, person1_stop );
Duration person1_duration = person1_interval.toDuration();
Minutes person1_minutes = person1_duration.toStandardMinutes();
Minutes person1_minutes_shortcut = Minutes.minutesBetween( person1_start, person1_stop ); // More direct, but you might want Interval object for other work.

// By default, the 'toString' on a Minutes object uses the ISO 8601 standard format for durations: https://en.wikipedia.org/wiki/ISO_8601#Durations
// In this case: PT120M
System.out.println( "person1_minutes: " + person1_minutes );
System.out.println( "person1_minutes_shortcut: " + person1_minutes_shortcut );
// Convert to a number.
int person1_minutesAsInt = person1_minutes.getMinutes();
System.out.println( "person1_minutesAsInt: " + person1_minutesAsInt );

// Person 2 - Not taken from question. I changed to earlier on this same day to show effect of Daylight Saving Time (DST).
DateTime person2_start = new DateTime( 2013, DateTimeConstants.NOVEMBER, 2, 23, 00, denverTimeZone ); // year, month, day, hour, minutes
DateTime person2_stop = new DateTime( 2013, DateTimeConstants.NOVEMBER, 3, 3, 00, denverTimeZone );
int person2_minutesAsInt = Minutes.minutesBetween( person2_start, person2_stop ).getMinutes();
// How many hours between 11 PM and 3 AM? 4 hours? Not on the day DST ends. 5 hours.
// 4 hours * 60 minutes = 240 minutes. 5 hours * 60 minutes = 300 minutes.
System.out.println( "person2_minutesAsInt: " + person2_minutesAsInt ); // 300 is correct answer.

When run…

person1_minutes: PT120M
person1_minutes_shortcut: PT120M
person1_minutesAsInt: 120
person2_minutesAsInt: 300

UTC Is Your Friend

Big tip: Use UTC.

Do not use zoned date-times for such work. Time zones, Daylight Saving Time, and such change often and unpredictably, even at the last-minute. Generally best to convert to UTC (Zulu time), then do all your work and storage. Convert back to zoned date-times only for display to users.

You may also want to store the zoned date-time as history. When you re-create the zoned date-time from UTC later, the zoned date-time may be different because of changes to the legal/political definitions of time zones and DST during the interim.

// Person 1 - CT: 12:00 PM | DT: 2:00 PM
DateTime person1_start = new DateTime( 2013, DateTimeConstants.NOVEMBER, 3, 12, 00, denverTimeZone ); // year, month, day, hour, minutes
DateTime person1_start_inUTC = person1_start.toDateTime( DateTimeZone.UTC );

System.out.println( "person1_start: " + person1_start );
System.out.println( "person1_start_inUTC: " + person1_start_inUTC );

When run…

person1_start: 2013-11-03T12:00:00.000-07:00
person1_start_inUTC: 2013-11-03T19:00:00.000Z

Upvotes: 2

Related Questions