1

I am working on an app that keeps track of travelling times

Consider the following example:

Trip 1 : Distance: 550km; TravelTime: 14:55 (14hours, 55 minutes)

Trip 2 : Distance: 230km; TravelTime: 12:25

Trip 3 : Distance: 140km; TravelTime: 10:44

I store the travel times as a long (Milliseconds from 1970-01-01). This all works fine, until I get a Travel time of more than 24 hours, or, obviously, when I want to display the total hours traveled.

Is there a way to format a date to a string that would show a date as an amount of hours and minutes, instead of the hours and minutes of the date?

As soon as I have a value of 26:10 (26 hours, 10 mins), the SimpleDateFormat will show "02:10" as it runs over to the next day, instead of the desired "26:10". Is there a way around this, or would I be better off creating a parser for that myself? Do not mind if that is the answer, just prefer not to re-invent the wheel ;)

Thanks in advance

Wessel du Plooy
  • 505
  • 5
  • 18

1 Answers1

1

You could calculate it manually instead:

long differenceInMilliseconds = ...;
long totalSeconds = differenceInMilliseconds / 1000;

int hours = totalSeconds / 3600;    
totalSeconds = totalSeconds % 3600;

int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I do it like this now, but was hoping there would be a formatting option for the SimpleDateFormat, so I can also easily use it for input. However, it got me thinking, and I should really do the input in a more controlled way. Thanks – Wessel du Plooy Nov 24 '12 at 13:29