Thomas N. Thomas N. - 1 year ago 69
Java Question

Java 8 LocalDate- determining the year of a yearless Feb-29 date?

My colleague and I have an interesting problem. We work with an old system that only returns date data in the format

. If the day/month is in the past for the current year, then we are to assume this date applies to next year. Otherwise it applies to the current year.

So today is
. If the system returned records with
, then that date translates to
. If the date reads
, then it translates to

However, it is unclear how to determine the year for
since it is a leap year. We cannot instantiate it with a year without the possibility of it throwing an error. We relied on a
when trying to create a
off it for the current year. If it catches, we assume it belongs to next year.

Is there a more kosher way to do this?

Answer Source
  • Parse the value as a MonthDay, as that's what you've got.
  • If the month-day is not February 29th, just handle it as normal
  • If it is February 29th, you need to special-case it:
    • Determine whether the current year is a leap year with Year.isLeap(long)
    • If it is:
    • If it's currently on or before Feb 29th, then the result is Feb 29th of this year
    • If it's currently after Feb 29th, you need rules to apply - you could choose March 1st of next year or Feb 28th of next year
    • If it's not (a leap year this year)
    • If it's currently on or before Feb 28th, again you need to rules to apply, probably returning March 1st or Feb 28th of this year
    • If it's currently after Feb 28th, then the date logically belongs to next year...
      • If next year is a leap year, the result is presumably Feb 29th of next year
      • If next year is not a leap year, again you need a rule

That's hopefully outlined the three "odd" conditions you need to account for - we don't have enough information to tell you what to do in those conditions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download