public static void main(String[] args) {
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
String begindate="2014-04-26";
String enddate="2016-05-08";
Date d1;
try {
d1 = date.parse(begindate);
Date d2 = date.parse(enddate);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
int a =(c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR))*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
String[] str = begindate.split("-");
int snf=Integer.parseInt(str[0]);
String month = str[1];
String[] etr = enddate.split("-");
String enf = etr[0];
int emonth = Integer.parseInt(etr[1]);
int monthnum = Integer.valueOf(month);
List<String> lists = new ArrayList<String>();
int t = 1;
for(int i=0;i <a;i++){
int num = monthnum + i;
if(num < 13){
if(num<10){
lists.add((snf+t-1)+"0"+num+"");
}else{
lists.add((snf+t-1)+""+num+"");
}
}else{
int mnum = num-12*t;
if(mnum<10){
lists.add((snf+t)+"0"+mnum+"");
}else{
lists.add((snf+t)+""+mnum+"");
}
if(mnum == 12){
t = t+1;
}
}
}
if(emonth<10){
lists.add(enf+"0"+emonth);
}else{
lists.add(enf+""+emonth);
}
for(int k=0;k<lists.size();k++){
System.out.println(lists.get(k));
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
java获取两个日期中月份
最新推荐文章于 2024-07-03 02:34:18 发布