I'm looking for a clean solution for formatting Date objects with optional parts. My date object can be a timestamp value, a time-only value or a date-only value.
Expected output
I would like to display the date value like this:
If the date's ms value is less than (86400*1000) then it means the value is a really a time-only value. If so display like this:
HH:mm:ss.SSS
If the date's value is exactly at midnight then it means it is a date-only value. If so display like this:
yyyy-MM-dd
Otherwise display like this:
yyyy-MM-dd HH:mm:ss.SSS
Solution constraints
Unfortunately I'm working with Date
as the input. Can't be changed. I'm only interested in formatting/rendering, not in parsing. External libraries are a hard sell in my organization.
For the purpose of formatting it would of course be acceptable to first convert to one of new Java 8 date/time objects if that will make the solution simpler.
Platform : Java 8 SE