0

I need to write a function that takes two timestamps and segment the time range in Days, Hours and Seconds in that order. To walk through an example, lets say the input to our function is

val start = 08-01-14 12:30:00 val end = 08-04-14 12:30:00

our function will return

 min ->  (08-01-14 12:30:00, 08-01-14 13:00:00)
 hour ->  (08-01-14 13:00:00, 08-01-14 24:00:00)
 day ->  (08-02-14  00:00:00, 08-03-14 24:00:00)
 hour -> (08-04-14  00:00:00, 08-04-14 12:00:00)
 min ->  (08-04-14 12:00:00, 08-04-14 12:30:00)

I have hand rolled an implementation that works but its a little kludgy, I am looking for a) is there a library that already does that? b) if not, what would be the best way to implement this?

Mansur Ashraf
  • 1,337
  • 3
  • 9
  • 12

1 Answers1

0

I believe Jodatime does support calculating time periods/ranges; http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html

I have done this before with Jodatime , it's a little bit technical, but as Jodatime is an AWESOME API for everything time-ish in Java all devs should use it - There was rumours of Jodatime replacing Javas old time-apis...

Introduction: http://www.joda.org/joda-time/

EDIT: Here's what you're looking for: http://joda-time.sourceforge.net/apidocs/org/joda/time/Interval.html IF you are using Jodatime interval can calculate the range/interval/period between two time-objects

TriXigT
  • 51
  • 4