According to two time periods, how many months in these two time periods
/ * * Create a date 2004-11-29 * * / Import java.text.SIMPLEDATEFORMAT; import java.util.calendar; import java.util.date; import java.util.gregoriancalendar;
/ ** * @Author wubai * * According to two time segments, how many months in these two time periods * * / public class test {public string getMonthlist (String BegindateStr, String EndDateStr) {// Specify to resolve time format SimpleDateFormat f = new SimpleDateFormat ( "yyyy-MM-dd"); // returns the month list String sRet = ""; // define variables Date beginDate = null; Date endDate = null; GregorianCalendar beginGC = null; GregorianCalendar Endgc = NULL; try {// Set string parse into date begindate = f.parse (begindateStr); enddate = f.parse (enddateStr); // Set calendar begingc = new Gregoriancalendar (); begingc.settime (begindate); Endgc = new gregoriancalendar (); endgc.settime (enddate); // until two times the same while (begingc.gettime (). Compareto (endgc.gettime ()) <= 0) {// accumulated string, single Quotation marking IF (SRET.EQUALS (")) {SRET = BeGingc.get (Calendar.y EAR) "-" 1);} else {sret = " begingc.get (calendar.year) " - " (begingc.get (Calendar). Month) 1);} // By the month, increase time begingc.add (Calendar.month, 1);} return sret;} catch (Exception E) {E.PrintStackTrace (); return null;}} / / Test use public static void main (string [] args) {test test = new test (); string tmp; tmp = Test.getMonthlist ("
2004-03-09 "," 2004-08-10 "); System.out.Println (" Month List: TMP); // The return result is as follows: // Month List: 2004-3, 2004-4, 2004-5, 2004-6, 2004-7, 2004-8 // That is to say, there is no 0 completion before the month, in order to make a conciseness, so there is no addition, you can make yourself to make yourself.}