Are there any Java libraries dealing with units of measurement except for JSR 275 (rejected and abandoned) and Units of Measure API (which doesn't seem to have any production-quality implementations)?
-
Did you ever find an answer? JScience seems full featured but with very little activity, seems like there would be a great need for this kind of library so it's surprising. – Rasmus Storjohann Mar 05 '14 at 00:59
-
@RasmusStorjohann No, I haven't. – Alexey Romanov Mar 05 '14 at 14:45
2 Answers
I have written a units library that does not use static typesetting (as in many practical applications I encountered this would have been more cumbersome that I would like such a library to be). It is designed to handle string based units as well as sharper defined units. Some of the supported features include:
conversions of values, e.g.:
Units.convert(3, "m", "mm"); Units.convert(3, SiBaseUnit.METER, "mm");
would both return
3000
.simplification of string based units, e.g.:
Units.simplify("kg^3 m^4 s^-6 A^-1");
would return
"J^2 T"
.finding the names of a unit in a specific context, e.g.:
Units.inContext("lx s", UnitContextMatch.COMPATIBLE, PhysicsContext.PHOTOMETRY)
would return a navigable set containing
("luminous exposure")
.supports SI units, binary units, imperial units, US customary units, atomic units, planck units and many more. The user can also easily define own units.
fully supports arbitrary logarithmic units, e.g.
LevelUnit.BEL.inReferenceTo(1, Unit.of("mV")); // automatically determines ref type -> root power LevelUnit.BEL.inReferenceTo(1, Unit.of("W"), LevelUnitReferenceType.POWER); // specify type explicitly Unit.of("ln(re 1 nA)") == LevelUnit.NEPER.inReferenceTo(1, Unit.of("nA")); // true
supports SI prefixes, binary prefixes and allows the user to easily implement own prefixes
Can handle unknown units if not relevant, e.g.:
Units.convert(3, "m^2 this_is_not_a_unit", "mm^2 this_is_not_a_unit");
would return
3e6
, as the unknown unitthis_is_not_a_unit
is the same on both sides of the conversion.for performance critical parts of the code one can obtain the conversion factor (if the conversion is purely multiplicative), e.g.:
Units.factor("kg", "t");
will return
1e-3
.Allows to check for equivalence, e.g.
Units.equivalent(1, "s", "min");
will return false, as
1min
is not the same as1s
. On the other hand, checking for convertibilityUnits.convertible("s", "min");
will return
true
.tightly integrated in the coordinates library (as of Java 16 this library still requires preview-features, but as of Java 17 it will production ready)
The constants are implemented via a Constant
interface that supports e.g.:
definition of own constants, e.g.
// (3 ± 0.2) mole Constant.of(3, 0.2, "mole");
chaining commands, e.g.
// constant with the distance travelled by light in vacuum in (2 ± 0) seconds as value PhysicsConstant.SPEED_OF_LIGHT_IN_VACUUM.mul(2, 0, SiBaseUnit.SECOND); // constant of the elementary charge per (electron) mass PhysicsConstant.ELEMENTARY_CHARGE.div(PhysicsConstant.ELECTRON_MASS); Constant c = Constant.of(3, 0.2, "mole"); PhysicsConstant.SHIELDING_DIFFERENCE_OF_T_AND_P_IN_HT.mul(c);
(simple) uncertainty propagation
the
Constant
interface provides default implementations for theTexable
interface from the jatex module, such that a constant can easily return proper LaTeX code.properly documented implementations for most of the physics constants as defined by NIST, as well as some mathematical constants.

- 251
- 3
- 5
https://github.com/unitsofmeasurement/uom-se from JSR 363
https://mvnrepository.com/artifact/org.unitsofmeasurement/unit-api/0.6.2-RC1
Hopefully your problem got solved approx. 4 yrs ago!

- 1
- 2
-
Link-only answers are generally [frowned upon](http://meta.stackexchange.com/a/8259/204922) on Stack Overflow. In time it is possible for links to atrophy and become unavailable, meaning that your answer is useless to users in the future. It would be best if you could provide the general details of your answer in your actual post, citing your link as a reference. – herrbischoff Jul 16 '17 at 22:40