<dependencies> <dependency> <groupId>javax.measure</groupId> <artifactId>unit-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>tec.units</groupId> <artifactId>unit-api</artifactId> <version>1.0-sp1</version> </dependency> </dependencies> <dependencies> <dependency> <groupId>tech.units</groupId> <artifactId>indriya</artifactId> <version>2.0</version> </dependency> </dependencies> import javax.measure.Quantity; import javax.measure.Unit; import javax.measure.quantity.Length; import javax.measure.spi.ServiceProvider; import static javax.measure.MetricPrefix.*; import static javax.measure.spi.Builder.*; import static systems.uom.common.USCustomary.MILE; import tech.units.indriya.quantity.Quantities; public class UnitConversionExample { public static void main(String[] args) { Unit<Length> mileUnit363 = ServiceProvider.current().getQuantityFactory(Length.class).create(1, MILE); Unit<Length> kmUnit363 = ServiceProvider.current().getUnitConverter().convert(mileUnit363).to(KILO(METER)); Quantity<Length> mileQuantity363 = ServiceProvider.current().getQuantityFactory(Length.class).create(10, mileUnit363); Quantity<Length> kmQuantity363 = ServiceProvider.current().getQuantityFactory(Length.class).create(0, kmUnit363); kmQuantity363 = ServiceProvider.current().getQuantityFactory(Length.class).create(kmQuantity363.getValue().doubleValue() + mileQuantity363.getValue().doubleValue(), kmUnit363); Quantity<Length> mileQuantityIndriya = Quantities.getQuantity(10, MILE); Quantity<Length> kmQuantityIndriya = INDRIYA.getQuantityFactory(Length.class).create(0, KILO(METER)); kmQuantityIndriya = INDRIYA.getQuantityFactory(Length.class).create(kmQuantityIndriya.getValue().doubleValue() + mileQuantityIndriya.getValue().doubleValue(), KILO(METER)); } }


上一篇:
下一篇:
切换中文