1

I am using javax.measure to convert user input to seconds that can vary anywhere from seconds to days, however converting from any unit to another doesn't work, here is what I have set up.

String units = "d";
double value = 30.0;
BaseUnit<Duration> unitType = new BaseUnit<Duration>(units);
UnitConverter toSeconds = unitType.getConverterTo(NonSI.DAY);
double s = toSeconds.convert(Measure.valueOf(value, unitType).doubleValue(unitType));

But I get this error

Exception in thread "main" javax.measure.converter.ConversionException: d is not compatible with s
at javax.measure.unit.Unit.searchConverterTo(Unit.java:259)
at javax.measure.unit.Unit.getConverterTo(Unit.java:248)
at FAMain.main(FAMain.java:18

This error comes up for any combination regardless if it is NonSI to NonSI. I can convert Length any way just fine, but for some reason Unit doesn't work.

pianoisland
  • 163
  • 1
  • 1
  • 10
  • what are you trying to do? code looks unreadable mess of different entities. What should be converted to what? – Iłya Bursov Jan 12 '16 at 22:16
  • Sorry, user input should be converted to seconds, and said input can range from seconds to days – pianoisland Jan 12 '16 at 22:26
  • what is user input? what are valid values for user to input? – Iłya Bursov Jan 12 '16 at 22:27
  • The input would be a string called units that is the abbreviation for the unit given, the value of the unit is stored in a double called value, as long as there is a double and a string the input is seen as valid. – pianoisland Jan 12 '16 at 22:41
  • I should add that this is just a snippet of the much larger function designed to show the error I am getting – pianoisland Jan 12 '16 at 22:44

1 Answers1

2

the main problem that you cannot just create new BaseUnit<Duration>("d"); and suppose that it will somehow become NonSI.DAY.

probably here is the starting point:

public static double toSeconds(final String unit, final double value) {
    switch (unit.toLowerCase()) {
    case "d":
    case "day":
        return NonSI.DAY.getConverterTo(SI.SECOND).convert(value);
    case "m":
    case "minute":
        return NonSI.MINUTE.getConverterTo(SI.SECOND).convert(value);
    case "s":
    case "second":
        return value;
    default:
        throw new IllegalArgumentException(unit);
    }
}
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • Thanks that works, but the confusing thing is that my syntax works for Length, what is the reason for this discrepancy? – pianoisland Jan 12 '16 at 23:20
  • @pianoisland cannot tell for sure without seeing code – Iłya Bursov Jan 12 '16 at 23:22
  • The length code is exactly what I posted above, just swap out Duration for Length, and any Length class in place of the time classes. Also Is there any way to fully rely on the javax.measure library to handle unit strings? in the event of getting strings like mins, min, sec, seconds etc? – pianoisland Jan 12 '16 at 23:25
  • @pianoisland I'm not aware about such mappings and doubt that it exists – Iłya Bursov Jan 12 '16 at 23:27