根据输入时间获取月份

public static void main(String[] args) {

try{

String beginDate="2022-02";

String endDate ="2023-01";


Date begin = DateUtils.parseDate(beginDate, "yyyy-MM");

Date end = DateUti1s.parseDate(endDate, "yyyy-MM");


Calendar c1 = Calendar.getInstance();

Calendar c2 = Calendar.getInstance();


c1.setTime(begin);

C2.setTime(end);


int startYear = c1.get(Calendar.YEAR);

int endYear = c2.get(Calendar.YEAR);

int starMonth = c1.get(Calendar.NONTH);

int endMonth = c2.get(Calendar.MONTH);


List List =new ArrayList<>();

for (int i= startYear; i<= endYear; i++){

String date="";

if (startYear == endyear){

for (int i= starMonth; 1<= endMonth;i++){

if(1<9){

date ="0"+(j+1);

}else {

date=""+(j+1);

}

list.add(date);

}

}else{

if (i == startyear) {

for (int j= starMonth; j< 12;j++)

if (j<9){

date ="0"+ (j+ 1);

} else {

date ="" + (j+1);

}

list.add(date);

}

}else if (i == endYear) {

for (int j=0; j<= endMonth; j++){

if (j<9){

date = "0"+ (j+ 1);

} else {

date = "" + (j+ 1);

}

list.add(date);

}

} else{

for (int j=0;j<12;j++){

if (j<9){

date = "0"+ (j+ 1);

} else {

date=""+(j+1);

}

List.add(date);

}

}

}

}

fot(int i=0; i< list.size();i++){

System.out.println(List.get(i));

} catch (Exception e){

e.printStackTrace();

}

}

你可能感兴趣的:(根据输入时间获取月份)