-4

In Java, there are the Duration and Period classes for spans of time.

Is there an equivalent class for length/distance?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Tad
  • 597
  • 5
  • 10
  • Not in the standard library. Besides, we don't know if you're measuring in imperial, metric, or Biblical distances (to name a few metrics). – Makoto Jan 08 '15 at 01:47
  • "Time" is an important concept in programming. Programs have to poll networks every _n_ milliseconds or something, cached data has expiration times, you might want a timeout when waiting for a user response, etc. Length/distance just doesn't have the same importance, so there's no reason for there to be a built-in type for that. Maybe there's a package somewhere for scientific units that also defines classes for mass, force, resistance, etc. But there wouldn't need to be anything like that for typical Java users. – ajb Jan 08 '15 at 01:49
  • 1
    @Makoto Yes, I was just looking for a "cubit" class the other day... – ajb Jan 08 '15 at 01:49
  • Mr. Polywhirl - No I don't mean a method, a mean a class similar to java.time.Duration. (http://docs.oracle.com/javase/8/docs/api/java/time/Duration.html) – Tad Jan 08 '15 at 06:39
  • ajb - Yes time is an important concept in programming. Also, length is an important concept in the real world and many programs are created to model some aspects of the real world. – Tad Jan 08 '15 at 06:45
  • @Makoto Yes, it seems that a Length class might have to do conversions between many different units of length. Perhaps a class designed to handle all of these conversions would be too unwieldily. (Conversions for Duration are more limited in scope.) – Tad Jan 08 '15 at 06:50
  • Asking for libraries is generally off-topic for Stack Overflow. I suggest you post on the [Software Recommendations Stack Exchange](http://softwarerecs.stackexchange.com). There you can explain your exact needs, specific to your industry. While date-time is commonly used across many kinds of apps, length/distance and other measurement systems tend to be less generally useful and more industry-specific. – Basil Bourque Jan 28 '17 at 22:17

1 Answers1

2

There are several: int, short, long, float, double, java.util.BigInteger... all of these would be appropriate for measuring distance. In many cases, these would also suffice for measuring time. The only reason a special class exists for measuring durations of time is that code that deals with time is often infuriatingly hard to get 100% right.

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
  • It might make sense to have special classes for measurement to help ensure that you get the units right (i.e. make sure you don't try to multiply distance * time and add the result to an acceleration), and to help with conversions. It wouldn't surprise me if someone had developed a package like that. – ajb Jan 08 '15 at 01:52
  • Approaching Darkness Fi - I'm working on a program where some of the user length data is entered in feet, some in miles, some in nautical miles, some in kilometers, etc. It would be nice if there were a class thad did the conversions automatically, similar to Duration.ofHours(), Duration.ofMinutes(), etc. Any time there are physical units involve, it is hard to get right 100% correct. (A spacecraft crashed on Mars due to software bugs in units: http://en.wikipedia.org/wiki/Mars_Climate_Orbiter) – Tad Jan 08 '15 at 06:33
  • ajb - There is a proposal for a general solution for handling units in Java. (https://www.jcp.org/en/jsr/detail?id=363) I'm looking for a more limited solution whose design is similar to java.time.Duration. – Tad Jan 08 '15 at 06:36
  • @Tad Yes, I'm familiar with the bug. I don't know of any programming language that supports a "units" feature, and I don't know of any standard library that provides unit conversions except for time. It's great that you want to prevent bugs like this, but you'll have to find your own system to do it. Usually either the units are included in variable names (eg `int lengthInMeters;`) or documented in comments (`/**length, measured in meters*/ int lenght;`) – ApproachingDarknessFish Jan 08 '15 at 06:38
  • @ApproachingDarknessFish Ada tried, sort of. It lets you define your own numeric types that can't be automatically converted, and one of the purposes of this was so that you couldn't say something like `x : distance; y : mass; ... y := x;`. The compiler would catch this. However, making this feature actually work for units proved to be really tough. I think a lot more built-in language support would have been needed, and the topic comes up in the Ada world regularly. – ajb Jan 08 '15 at 20:28
  • @ajb - Haskell has a couple of options, one being Dimensional https://hackage.haskell.org/package/dimensional – Tad Jan 09 '15 at 03:13
  • @ApproachingDarknessFish - I didn't know that about Ada. Haskell has some nice solutions provided by libraries, one of which is "dimensional" (https://hackage.haskell.org/package/dimensional). – Tad Jan 10 '15 at 02:59