I'm looking for any way to create a fixed day to month using JodaTime. I have a JSpinner to set integer values and I want get this value and create the Date. For example: JSpinner has 15, I want to create the date
LocalDate class represents a date-only value without time-of-day and without time zone.
You can specify the year, month, and day-of-month.
LocalDate ld = LocalDate.of( 2016 , 1 , 15 );
You can switch months. The java.time classes use the Immutable Objects pattern, so a new fresh object is generated based on values of the original.
LocalDate september = ld.withMonth( 9 ); // 1-12 for January-December.
You can add/subtract months.
LocalDate nextMonth = ld.plusMonths( 1 ); LocalDate priorMonth = ld.minusMonths( 1 );
You can take any LocalDate and adjust the day to the 15th.
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ); LocalDate fifteenthThisMonth = today.withDayOfMonth( 15 );
Trap for a
DateTimeException being thrown in the case of a day-of-month number invalid for that month, such as 31 for February.
If you must use Joda-Time, it offers a
LocalDate class quite similar to
LocalDate ld = new LocalDate( 2016 , 1 , 7 ); LocalDate fifteenthSameMonth = ld.withDayOfMonth( 15 );