1.代码补全。用户输入两个日期,程序将判断两个日期的大小关系,以及两日期间隔的天数。public class CompareDate { public static void main(String args[ ]) { Scanner scanner = new Scanner(System.in); System.out.println("输入第一个年,月,日数据"); System.out.print("输入年份"); short yearOne = scanner.nextShort(); System.out.print("输入月份"); byte monthOne = scanner.nextByte(); System.out.print("输入日期"); byte dayOne = scanner.nextByte(); System.out.println("输入第二个年,月,日数据"); System.out.print("输入年份"); short yearTwo = scanner.nextShort(); System.out.print("输入月份"); byte monthTwo= scanner.nextByte(); System.out.print("输入日期"); byte dayTwo = scanner.nextByte(); Calendar calendar = 【代码1】 //初始化日历对象【代码2】 //将calendar的时间设置为yearOne年monthOne月dayOne日 long timeOne =【代码3】 //calendar表示的时间转换成毫秒 calendar.set(yearTwo,monthTwo-1,dayTwo); long timeTwo=calendar.getTimeInMillis(); Date date1 = 【代码4】 // 用timeOne做参数构造date1 Date date2 =new Date(timeTwo);
时间: 2024-01-25 20:05:05 浏览: 80
ASP.NET如何获取两个日期之间的天数
代码补全如下:
```java
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class CompareDate {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入第一个年,月,日数据");
System.out.print("输入年份");
short yearOne = scanner.nextShort();
System.out.print("输入月份");
byte monthOne = scanner.nextByte();
System.out.print("输入日期");
byte dayOne = scanner.nextByte();
System.out.println("输入第二个年,月,日数据");
System.out.print("输入年份");
short yearTwo = scanner.nextShort();
System.out.print("输入月份");
byte monthTwo = scanner.nextByte();
System.out.print("输入日期");
byte dayTwo = scanner.nextByte();
Calendar calendar = Calendar.getInstance(); //初始化日历对象
calendar.set(yearOne, monthOne - 1, dayOne); //将calendar的时间设置为yearOne年monthOne月dayOne日
long timeOne = calendar.getTimeInMillis(); //calendar表示的时间转换成毫秒
calendar.set(yearTwo, monthTwo - 1, dayTwo);
long timeTwo = calendar.getTimeInMillis();
Date date1 = new Date(timeOne); //用timeOne做参数构造date1
Date date2 = new Date(timeTwo);
if(date1.before(date2)) {
System.out.println("第一个日期早于第二个日期");
} else if(date1.after(date2)) {
System.out.println("第一个日期晚于第二个日期");
} else {
System.out.println("两个日期相同");
}
long interval = Math.abs(timeOne - timeTwo) / (1000 * 60 * 60 * 24);
System.out.println("两个日期间隔的天数为:" + interval);
}
}
```
阅读全文