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?
 
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.