My colleague and I have an interesting problem. We work with an old system that only returns date data in the format ddMMM
. 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 4/30/2015
. If the system returned records with 12MAR
, then that date translates to 3/12/2016
. If the date reads 07MAY
, then it translates to 5/7/2015
.
However, it is unclear how to determine the year for 29FEB
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 try/catch
when trying to create a LocalDate
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?
The year for a particular LocalDate can be obtained using the getYear() method in the LocalDate class in Java. This method requires no parameters and it returns the year which can range from MIN_YEAR to MAX_YEAR.
The isLeapYear() method of java. time. LocalDate verifies if the year in the current object is a leap year according to the ISO proleptic calendar system rules, returns true if so, else returns false.
The idea is to use methods of LocalDate class to get the day, month, and year from the date. The getDayOfMonth() method returns the day represented by the given date, getMonth() method returns the month represented by the given date, and getYear() method returns the year represented by the given date.
MonthDay
, as that's what you've got.Year.isLeap(long)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With