unitsofmeasurement / indriya

JSR 385 - Reference Implementation
Other
115 stars 40 forks source link

Custom default QuantityFormat and UnitFormat #366

Open puigru opened 2 years ago

puigru commented 2 years ago

Currently, both Quantity#toString and Unit#toString default to a built-in formatter, SimpleQuantityFormat and SimpleUnitFormat. I believe it would be useful to provide a way to set a custom formatter globally, especially for Quantity. Meaning any time a Quantity is converted to a String within the application it would use the provided formatter.

https://github.com/unitsofmeasurement/indriya/blob/cb717f73e742f0bb6f7defdacef3957b6dc7e7b4/src/main/java/tech/units/indriya/AbstractQuantity.java#L295-L297

keilw commented 2 years ago

We did try this, check out the second ticket here #2, but it had so many unforseen side-effects on formatting and parsing, that we abandoned it. Feel free to fork the code and demonstrate it works, then we may revisit it, but given the built-in Date/Time library of the JDK has ultra-primitive toString() functionality in most of its core types like DateTime, LocalDateTime, etc. and Instant does exactly the same default formatting:

    public String toString() {
        return DateTimeFormatter.ISO_INSTANT.format(this);
    }

we do not feel a strong need in another value-based Java API to go beyond what the JDK felt was necessary.