JakeWharton/ThreeTenABP

TemporalAccessor API different from Java8

msrd0 opened this issue · 6 comments

msrd0 commented

For those that are trying to derive their own class from TemporalAccessor might notice that in Java8 (see the source code here) the get, query and range methods are default methods means one does not have to override them. However, on Android I have to override those methods.

I know that the interface is marked as a framework-level interface, however reading the javadoc makes me believe that one does not necessarily want to avoid users creating subtypes but rather make them use specific types instead of generic ones. Please correct me if I'm wrong or point me to a better solution if there is one. The reason why I'm trying to inherit from TemporalAccessor is because DateTimeFormatter.format takes a TemporalAccessor as an argument.

Out of curiosity: What kind of problem do you want to solve? Which kind of TemporalAccessor-subclass do you have in mind?

msrd0 commented

@MenoData I'm trying to create a class that handles time differences and that can be printed with a DateTimeFormatter

msrd0 commented

@JakeWharton Thanks, I reported it: ThreeTen/threetenbp#78

@msrd0 Oh this is not a good idea because a TemporalAccessor is NOT a duration (or any other kind of TemporalAmount). And a DateTimeFormatter is not designed for printing durations.

You will need a printer for TemporalAmount instead, something like what I have already written in my time library Time4J, either pattern-based or localized for many languages. For Android, the version Time4A uses its own duration types only (which are more powerful however) because JSR-310 is not available below api-level 26.

I doubt if reinventing the wheel is necessary ;-), and we also have the Android-class DateUtils with its admittingly terrible API.

msrd0 commented

@MenoData Well, I don't really care if DateTimeFormatter was designed for printing durations because it works like I expect it. I agree that Android APIs are crap, but I might check out Time4J if that does what I need (I already have a somewhat complete class for the difference that works on Oracle JDK so I just looked for java.time on Android).