AndreaNobili AndreaNobili - 12 days ago 4
Java Question

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

it is my first time with Joda Time library and I have the following doubt.

I have to check if a specific data is not the last day of an year (it is not important the year, the data have tobenthe 31/12/XXXX).

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 to not be the last day of year). I think that it should do works fine.

But I am asking if Joda Time provides some neater method to do it. Exist?

Answer

Currently Joda Time don't have direct implementation of getLastDayOfYear(), we have to use logical way to handle the last day of year, one way you tried or you can use alternate way as below

 public int getDayOfYear() { //This is library method
    return getChronology().dayOfYear().get(getMillis());
}

if(!getDayOfYear() <=366) { //Considering leap year
System.out.println("It is not the end of year !!!");
}
Comments