A date can be formatted using a e.g. SimpleDateFormat(formatPattern, locale).
Is it somehow possible to determine the time period (in seconds) that is represented by formatPattern given a date? E.g. if we have
Date date = new Date(1286488800);
String formatPattern = "yyyy";
is it possible to determine the length of the year (in seconds) represented by formatPattern and in which date lies?
I believe to have found a proper solution for this. It works for me, although I am not sure and haven’t tested whether this works precisely in every situtions (e.g. with leap seconds). If you have suggestions for improvement, feel free to post them. Here is the code:
It basically works by comparing the calender fields of a calendar set to a date that uses all fields (oddestDate) with a calendar set to the same date but formatted, printed, and parsed again by the formatPattern. To compensate at least for leap years, an inputDate is also required.