0

I have to get the months in between my Maxtime to Mintime.This code below works fine till Feb.After Feb it never return Jan.

long max = 1410326172L;//10-Sep-2014
long min = 1388553371L;//1-Jan-2014
    Date maxDate = new Date(max);
    Date minDate = new Date(min);
while (maxDate.getTime() >= minDate.getTime()) {
Calendar cal = getCalendarWithTime(maxDate);            
        long firstDateMillis = DateHelper.getFirstDate(cal);//This is to get the First Date of an particular month.
        long lastDateMillis = DateHelper.getLastDate(cal);//This is to get the Last Date of an particular month
        // substract one month from max date  
        cal.add(Calendar.MONTH, -1); // moving one month in past
        maxDate = cal.getTime();
        System.out.println("------------------"+new SimpleDateFormat("MM").format(maxDate)) 
    }
Runi
  • 9
  • 8

1 Answers1

0
long max = 1410326172L;//10-Sep-2014
long min = 1388553371L;//1-Jan-2014
Date maxDate = new Date(max);
Date minDate = new Date(min);
while (maxDate.getTime() >= minDate.getTime()) {
Calendar cal = getCalendarWithTime(maxDate);            
    long firstDateMillis = DateHelper.getFirstDate(cal);//This is to get the First Date of an particular month.
    long lastDateMillis = DateHelper.getLastDate(cal);//This is to get the Last Date of an particular month

    int test = Integer.parseInt(new SimpleDateFormat("MM").format(maxDate));
        // substract one month from max date
        cal.add(Calendar.MONTH, -1);// moving one month in past
        if (test == 2) {
            if (c == 0) {
                cal.set(Calendar.MONTH, 1);//month of Feb
                maxDate = cal.getTime();
                c++;
            } else {
                cal.set(Calendar.DATE, 30);
                cal.set(Calendar.MONTH, 0);//month of Jan
                maxDate = cal.getTime();
            }
        } else {
            maxDate = cal.getTime();
        }
}

This is an simple logic but it solved my issue

Runi
  • 9
  • 8