javadatejodatime

calculate Third monday of every three month in Joda-Time in Java?


LocalDate date1 = new LocalDate(2015, 3, 22);           
LocalDate date2 = new LocalDate(2015, 9, 30);           
PeriodType monthDay = PeriodType.yearMonthDay().withYearsRemoved();

Period difference = new Period(date1, date2, monthDay);             
int months = difference.getMonths();            
int days = difference.getDays();                
int alertMonth = 2;             
int intervalLoop = date2.getMonthOfYear() / alertMonth ;            
for(int i=date1.getMonthOfYear();i<date2.getMonthOfYear();i++){             
         int intervalTime = i * alertMonth;             
         if(intervalTime >13){

               return;          
         }else{
  LocalDate d = getNDayOfMonth( DateTimeConstants.WEDNESDAY, 2, i, 2015);
  System.out.println("month----> "+i+" "+d);

Solution

  • This code may help..

    DateTime urDate = DateTime(date);                               //Your Date time
    DateTime afterThreeMonths = urDate.plusMonths(6);               //Add 3 months to your Date
    afterThreeMonths.withDayOfWeek(DateTimeConstants.MONDAY);       //Find next monday  
    afterThreeMonths.plusWeeks(2);                                  //Add two more weeks to the first monday.
    

    NB: I never run this code. Try and find it out urself.