0

I have a requirement to implement a DateTime and Duration object that keeps track of its value using a fractional value. For all intents and purposes, these types should be equivalent to the Joda's DateTime and Duration types, except the millis not 'long'.

How would I go about implementing these types so they are consistent and I get to keep all the niceties Joda brings with it? Is it even possible?

EDIT: To remove ambiguity, by "fractional value", I mean actual fractions, i.e. rational numbers.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
milin
  • 414
  • 4
  • 14
  • 1
    What do you mean by "fractional value"? Do you mean some fixed number of decimal places of seconds? Or do you mean actual fractions? – Dawood ibn Kareem Jul 11 '22 at 22:43
  • I mean actual fractions, rational numbers, defined with a (long) numerator and denominator. – milin Jul 11 '22 at 22:45
  • 1
    If I had to do this, I'd start with the source for either the Joda classes or the java.time equivalents, and modify everything to store the seconds as a fraction. Trying to develop these classes starting from nothing would be a bit too difficult. You probably want to think about DateFormat type classes as well, so you can print out your dates and your durations sensibly. – Dawood ibn Kareem Jul 11 '22 at 23:05
  • Your Question is not clear. Can you give some example data and code? – Basil Bourque Jul 12 '22 at 00:43
  • @BasilBourque What do you find unclear? OP wants to be able to store dates with a fractional time component, e.g. 1/3 of a second. By somehow incorporating a `Fraction` class into classes that work the same as Joda's `Duration` and `DateTime`. – Dawood ibn Kareem Jul 12 '22 at 04:15
  • @DawoodibnKareem I see no mention of a `Fraction` class in the question. – Basil Bourque Jul 12 '22 at 05:56

1 Answers1

1

You probably can't get all of the "niceties Joda brings with it."

What you could do -- which Joda might or might not play well with -- is use java.time.Duration, which has nanosecond precision, which is good enough for any application I can conceive of.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Firstly, I cannot use java.time.Duration since I'm, unfortunately, stuck working on an antiquated Java version. And secondly, it's not a matter of millisecond vs nanosecond precision. My domain of usage naturally operates in the rational numbers, so there's no way I can work with anything less. (In all fairness, were I not aware of the requirements of this specific programming task, I would probably be in the "I see no need for greater precision" mindset as well. You live, you learn.) – milin Jul 11 '22 at 22:38
  • All that being said, thank you for your comment. I might just have to come to terms with the fact I will need to go back and forth between Joda types and my types whenever I need a feature from the opposite "camp". – milin Jul 11 '22 at 22:47
  • 2
    @milin See [*ThreeTen-Backport*](https://www.threeten.org/threetenbp/). This library brings most of the *java.time* functionality to Java 6 and Java 7, with virtually identical API. So eventually switching to *java.time* will involve little more than changing the `import` statements. FYI, all three projects (*Joda-Time*, *java.time*, & *ThreeTen-Backport*) are led by the same man, Stephen Colebourne. – Basil Bourque Jul 12 '22 at 00:38