Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a smarter way to check if a Joda-Time DateTime object does not represent the last day of the year?

I am using Joda-Time and I would like to check if a specific date is not the last day of a year (in other words the date should not be 31/12/XXXX). It should work for any year.

To implement this check I have done something like this:

DateTime dataSecondoMovimento = new DateTime(mappaQuote.get(2).getDatariferimentoprezzo());

if(!(dataSecondoMovimento.getMonthOfYear() == 12 && dataSecondoMovimento.getDayOfMonth() == 31)) {
    System.out.println("It is not the end of year !!!");
}

So basically I am checking if the month is not December and if the day is not 31 (both have to be true for it not to be the last day of year). I think that this should work fine.

My question: does Joda-Time provide a neater way of doing this? Does there exist a method of checking if a specific date is the last day of the year?

like image 636
AndreaNobili Avatar asked Dec 18 '22 11:12

AndreaNobili


1 Answers

IMHO, your way is quite practical.

The only thing comes to my mind is

dataSecondoMovimento.plusDays(1).getDayOfYear() == 1

which is shorter but hardly more effective

P.S.: I've found a small benefit in this approach -- it will work for slightly improbable situation with non-Gregorian calendar which doesn't have 12 months or 31 days in the last one!

like image 78
Sergey Grinev Avatar answered Apr 19 '23 23:04

Sergey Grinev