输出两个时间以及两个时间相差的月份
class="java" name="code"> public void getYMArray(){ Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(changStringToDate("2016-12-01")); c2.setTime(changStringToDate("2018-01-01")); Integer beginYear = c1.get(Calendar.YEAR); Integer beginMonth = c1.get(Calendar.MONTH)+1; Integer endYear = c2.get(Calendar.YEAR); Integer endMonth = c2.get(Calendar.MONTH)+1; Integer difMonth = (endYear - beginYear) * 12 + endMonth - beginMonth; if(difMonth>1){ String[] dateYM=new String[1+difMonth]; Integer startmonth=1; for(int i=0;i<=difMonth;i++){ startmonth=startmonth+beginMonth; if(startmonth<=12){ if(startmonth<10){ dateYM[i]=beginYear.toString()+"0"+startmonth.toString(); }else{ dateYM[i]=beginYear.toString()+startmonth.toString(); } }else{ beginYear=beginYear+1; if(beginMonth<10){ dateYM[i]=beginYear.toString()+"0"+beginMonth.toString(); }else{ dateYM[i]=beginYear.toString()+beginMonth.toString(); } beginMonth=1; startmonth=0; } } System.out.println(dateYM); }else if(difMonth==1){ String[] dateYM=new String[2]; dateYM[0]=beginYear.toString()+beginMonth.toString(); dateYM[1]=endMonth.toString()+endMonth.toString(); System.out.println(dateYM); } }
?判断两个时间是否在同一个月
public void compareToTwoDate(){ String start="2016-01"; String end="2016-02"; if(start.split("-")[0].equals(end.split("-")[0])){ if(start.split("-")[1].equals(end.split("-")[1])){ System.out.println("true"); }else{ System.out.println("false"); } }else{ System.out.println("false"); } }
?