Relative time formatting

Thoughts By 5 years ago

Recently I was given the requirement of displaying relative time in certain areas of the app. Relative time formatting is displaying the time since something occurred relative to the current time i.e. the event occurred 5 minutes ago. As soon as I was given the requirements the builder part of my brain kicked in and before I knew it I was piecing together all the bits and pieces of code in my head, when an obvious thought struck me. Surely this type of request isn’t out of the ordinary, more to the point surely there had to be a library already written to handle it. A short time after that thought Google led me straight to the perfect library.

That perfect library which saved a whole lot development time (may be an exaggeration) is called Pretty Time and can be found here. What makes Pretty Time so useful is that it’s so simple to use. Customisation is as simple as extending the current TimeUnit with the text you want to display. The best part about all of this is that the library has automatic locale support built right in so there’s no worrying about string resources holding different languages. The library itself is very small and  efficient. I was able to use it for formatting inside a Android ListAdapter with no noticeable performance issues to the ListView itself even on an old Motorola Milestone.

So if you need relative time formatting in an Android or Java app I’d definitely recommend checking it out.