Unit of measurement API in Java?

6.4k views Asked by At

JSR-275 has been rejected, the Units of Measurement API for Java project is a set of interfaces, but haven't found an open source implementation.

On this post: Which jsr-275 units implementation should be used? the project owner mentions the implementation was going to be ready by the end of last year on JScience, but didn't find anything there to convert between weight or length units and when I looked for JScience on https://maven.java.net/, I found it, but the JAR wasn't even in the directory https://maven.java.net/content/repositories/snapshots/org/jscience/jscience/5.0-SNAPSHOT/, so I had to get it from somewhere else.

Has this project been left behind? And is there currently an implementation for conversion of Units of measurement in Java and even perhaps a Maven repo?

4

There are 4 answers

0
Vineet Reynolds On BEST ANSWER

As far as I know, JScience is very much alive. The project is currently being migrated to Java.net, and the migration is not complete. That is most likely the reason why you are unable to see snapshot JARs for 5.0. In fact, the most recent snapshot was prepared only after resolution of a particular configuration problem reported in the Java.net JIRA. May be you ought to wait a few days, or probably send a mail to the project administrator on what JScience POM must be used in the interim.

0
James Mudd On

Update on this JSR-363 Units of Measurement API was completed in 2016 and provides a fairly complete UoM API. This has now been superseded by JSR-385 Units of Measurement API 2.0. You can find the code behind the API and implementation on Github here https://github.com/unitsofmeasurement.

Here is a simple conversion example using UoM API 2.0

import tech.units.indriya.quantity.Quantities;
import javax.measure.Quantity;
import javax.measure.quantity.Length;

import static javax.measure.MetricPrefix.CENTI;
import static tech.units.indriya.unit.Units.METRE;

class SimpleUnitExample {
    public static void main(String[] args) {
        Quantity<Length> lengthQuantity = Quantities.getQuantity(25, METRE);
        System.out.println(lengthQuantity.to(CENTI(METRE)));
    }
}

With a dependency on tech.units:indriya:2.0.4 will print 2500 cm.

You can find lots more examples in this repo uom-demos.

4
Werner Keil On

Unit-API (unitsofmeasurement.org) is the successor to JSR-275.

The most active implementation at the moment is Eclipse UOMo

0
Piotr_J. On

If you look for something simple, you can check out my project. I have created an opensource Unitility for working with physical quantities and units of measurements including easy conversion between them. Example of usage:

// Creating temperature instance of specified units
Temperature temperature = Temperature.ofCelsius(20.5);         // {20.50 °C}

// Getting converted value for calculations
double valueInCelsius = temperature.getInCelsius();            // 20.50 °C
double valueInFahrenheits = temperature.getInFahrenheits();    // 68.90 °F

// Checking property current unit, value, and value in base unit
TemperatureUnit unit = temperature.getUnit();                  // CELSIUS
TemperatureUnit baseUnit = unit.getBaseUnit();                 // KELVIN 

// Changing property unit and converting back to base unit
Temperature tempInFahrenheits = temperature.toUnit(TemperatureUnits.FAHRENHEIT);
Temperature tempInKelvins = temperature.toBaseUnit(); 

Additionally, logical and basic math operations are supported within quantities of the same type:

// Logic operation example:
Temperature smallerTemp = Temperature.ofCelsius(-20.0);             
Temperature greaterTemp = Temperature.ofCelsius(0.0);   
smallerTemp.isLowerThan(greaterTemp);         // true                       
greaterTemp.isGreaterThan(smallerTemp);       // true

// Math operation example:
Temperature sourceTemperature = Temperature.ofCelsius(20);
Temperature temperatureToAdd = Temperature.ofKelvins(293.15);
Temperature actualTemperature = sourceTemperature.add(temperatureToAdd); // {40°C}

Maybe this will be helpful if you are looking for something simple. It supports a selection of most typical physical quantities and its units for SI and IMPERIAL units.

  • If you need any specific unit or quantity to be added - just let me know.
  • I have provided modules for Spring and Quarkus support to preconfigure object mappers for easier use in web applications,

Maven repository: https://mvnrepository.com/artifact/com.synerset/unitility-core

I hope this helps.