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

I use Joda-Time , and I would like to check if a particular date is not the last day of the year (in other words, the date should not be 31/12 / XXXX). He must work for any year.

To do this check, I did 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 check if the month is a month, and if the day is not 31 (both must be true so that it is not the last day of the year). I think this should work fine.

My question is: Does Joda-Time make an easier way to do this? Is there a method for checking if a particular date is the last day of the year?

+4
5

Joda Time getLastDayOfYear(), , , , , .

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

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

, .

, , -

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

,

P.S.: - , 12 31 !

+4

DateTime.Property

- , , , , DateTime.Property dayOfYear, , .

private boolean isLastDayOfYear(DateTime dateTime) {
    return dateTime.dayOfYear().get() == dateTime.dayOfYear().getMaximumValue();
}

:

if (!isLastDayOfYear(dataSecondoMovimento)) {
    System.out.println("It is not the end of year !!!");
}
+3

- , , :

DateTime dataSecondoMovimentoPlusOneDay = dataSecondoMovimento.plusDays(1);

boolean yearChanged = dataSecondoMovimento.getYear() != dataSecondoMovimentoPlusOneDay.getYear();
+2

TL;DR

MonthDay.from( zdt ).equals( 
    MonthDay.of( 
        Month.DECEMBER , 
        Month.DECEMBER.maxLength() 
    )
)

java.time

Joda-Time java.time, Java.

MonthDay . , - 31 . . , , 31.

static final public MonthDay MONTHDAY_END_OF_YEAR = MonthDay.of( Month.DECEMBER , Month.DECEMBER.maxLength() );

Joda-Time DateTime java.time ZonedDateTime.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( z );

MonthDay.

MonthDay md = MonthDay.from( zdt );

.

Boolean isLastDay = md.equals( MONTHDAY_END_OF_YEAR );

TemporalAdjuster

, TemporalAdjuster: TemporalAdjusters.lastDayOfYear() ( ').

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtNow = ZonedDateTime.now( z );
ZonedDateTime zdtLastDayOfYear = zdtNow.with( TemporalAdjusters.lastDayOfYear() ) ;
MonthDay mdYearEnd = MonthDay.from( zdtLastDayOfYear );

.

System.out.println( "zdtNow: " + zdtNow );
System.out.println( "zdtLastDayOfYear: " + zdtLastDayOfYear );
System.out.println( "mdYearEnd: " + mdYearEnd );

zdtNow: 2016-11-26T15: 44: 06.449-05: 00 [/]

zdtLastDayOfYear: 2016-12-31T15: 44: 06.449-05: 00 [/]

mdYearEnd: --12-31

IdeOne.com.


java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .

+2

Source: https://habr.com/ru/post/1661586/


All Articles