Military Time Difference in Java

tl;dr

Duration
.between(
    LocalTime.parse( "0150" , DateTimeFormatter.ofPattern( "HHmm" ) ) ,
    LocalTime.parse( "0240" , DateTimeFormatter.ofPattern( "HHmm" ) ) 
)
.toString()

PT50M

Details

Perhaps you are just working on homework. If so, make that clear in your Question.

But you should know that Java provides classes for this purpose.

java.time

The modern approach uses the java.time classes. These classes work in 24-hour clock by default.

LocalTime

For a time-of-day without a date and without a time zone, use LocalTime.

    LocalTime start = LocalTime.of( 1 , 50 );
    LocalTime stop = LocalTime.of( 2 , 40 );

Duration

Calculate elapsed time as a Duration.

    Duration d = Duration.between( start , stop );

Generate text representing that Duration value. By default, standard ISO 8601 format is used.

    System.out.println( d );

PT50M

Parts

You can extract the parts if desired.

int hours = d.toHoursPart() ;
int minutes = d.toMinutesPart() ;

Parsing

To parse your HHMM format provided by your users, use DateTimeFormatter.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "HHmm" ) ;
LocalTime lt = LocalTime.parse( "0150" , f ) ;

Zones

Be aware that working only with time-of-day without the context of date and time zone can lead to incorrect results. If working in UTC, no problem. But if your time-of-day values are actually intended to represent the wall-clock time of a particular region, then anomalies such as Daylight Saving Time (DST) will be ignored by use of LocalTime only. In your example, there may be no two o'clock hour, or two o'clock have have been repeated, if occurring on a DST cut-over date in the United States.

If you implicitly intended a time zone, make that explicit by applying a ZoneId to get a ZonedDateTime object.

LocalDate ld = LocalDate.of( 2018 , 1 , 23 ) ;
ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z );
…
Duration d = Duration.between( zdt , laterZdt ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


So to figure out elapsed time between two 24 hour times you need to transfer to total minutes now the modulo functions you are using will not work as 1 hour = 60 min not 100 min.

So first to transfer all hours/minuets to minute time.

int ftminuets = 0;
int stminuets = 0;
int totalmin = 0;
int elapsed = = 0
while(fTime >= 100)
{
     ftminuets += 60;
     fTime -= 100;
}
ftminuets += fTime; //gets remaining minuets from fTime.

while(sTime >= 100)
{
    stminuets += 60;
    sTime -= 100;
}
stminuets += sTime;  //gets remaining minuets from sTime.

//time to subtract
totalmin = stminuets - ftminuets;
//now total min has the total minuets in what can be considered to be 60 min increments.  Now just to switch it back into a 24 hours time.

while(totalmin >= 60)
{
    elapsed += 100;
    totalmin -= 60;
}
elapsed += totalmin; //get rest of the minuets.

elapsed should now have the elapsed time in 24 hours time.


I've answered my own question, the solution was that I seperated the hours from the minutes part (e.g. 1159 to 11 and 59), multiplied the hours to get the minutes and added that to the rest of the minutes.

this.fTimeInMin = ((fTime / 100) * 60) + (fTime % 100);
this.sTimeInMin = ((sTime / 100) * 60) + (sTime % 100);

And then, in the getHours() and the getMinutes() method transformed the minutes to hours and minutes:

public int getHours() {
    return Math.abs((this.fTimeInMin - this.sTimeInMin) / 60);
}

public int getMinutes() {
    return Math.abs((this.fTimeInMin - this.sTimeInMin) % 60);
}

Tags:

Java