在java中计算月份差

来源:百度知道 编辑:UC知道 时间:2024/06/22 02:09:02
我要算的是比如说2008年08月减去2007年11月的结果,意思是说某一年和某一年之间的月份差,要怎么计算?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test21 {

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入第一个年份");
int yearFirst = Integer.parseInt(br.readLine());
System.out.print("输入第一个月份");
int monthFirst=Integer.parseInt(br.readLine());
System.out.println("输入第二个年份");
int yearSecond = Integer.parseInt(br.readLine());
System.out.print("输入第二个月份");
int monthSecond=Integer.parseInt(br.readLine());

int monthsub = (yearFirst-yearSecond)*12+(monthFirst-monthSecond);
System.out.println("相差的月份"+monthsub);
}
}