two given year-month

How do I find in-between year month between two given  year-month ?

Example:

Input:
2014-08  ,  2015-02

Output:
2014-09
2014-10
2014-11
2014-12
2015-01

Could you please suggest a simple and easy solution to this ?
cofactorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
Try something like this:

	private void dumpMonths(String start, String end) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM") ;
		Date startDate = format.parse(start) ;
		Date endDate = format.parse(end) ;

		Calendar startCal = Calendar.getInstance() ;
		startCal.setTime(startDate);

		Calendar endCal = Calendar.getInstance() ;
		endCal.setTime(endDate);

		while (startCal.get(Calendar.YEAR) < endCal.get(Calendar.YEAR) || startCal.get(Calendar.MONTH) < endCal.get(Calendar.MONTH) - 1) {
			startCal.add(Calendar.MONTH, 1);
			Date date = startCal.getTime() ;
			System.out.println(format.format(date)) ;
		}
	}

Open in new window


Hope that helps,

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mccarlIT Business Systems Analyst / Software DeveloperCommented:
And if you are using (or can use) Java 8 you can do it quite easily...

import java.time.YearMonth;

public class TestYearMonth {
    public static void main(String[] args) {
        YearMonth start = YearMonth.parse("2014-08");
        YearMonth end = YearMonth.parse("2015-02");
        
        YearMonth ym = start.plusMonths(1);
        while (ym.isBefore(end)) {
            System.out.println(ym);
            ym = ym.plusMonths(1);
        }
    }
}

Open in new window

0
cofactorAuthor Commented:
I am using java 6
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
That's a shame, because you can probably see how much easier it is.

Anyway, Doug's solution should work fine for Java 6.


Otherwise, a solution out of left field, if all you have to do is generate those year month strings (ie. you don't need to do further arithmetic), you can with some simple String manipulation, such as...

public class TestYearMonth {
    public static void main(String[] args) {
        int start = parse("2014-08");
        int end = parse("2015-02");
        
        for (int i = start + 1; i < end; i++) {
            System.out.println(String.format("%04d-%02d", i / 12, i % 12 + 1));
        }
    }

    private static int parse(String string) {
        return Integer.valueOf(string.substring(0, 4)) * 12 + Integer.valueOf(string.substring(5)) - 1;
    }
}

Open in new window

0
CEHJCommented:
You only need one Calendar doing it like that:

    private void dumpMonths(String start, String end)
        throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date startDate = format.parse(start);
        Date endDate = format.parse(end);

        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);

        Date date = startDate;

        while (true) {
            cal.add(Calendar.MONTH, 1);
            date = cal.getTime();

            if (date.equals(endDate)) {
                break;
            }

            System.out.println(format.format(date));
        }
    }

Open in new window

0
cofactorAuthor Commented:
Excellent
0
CEHJCommented:
Check the bytecode
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.