###### weixin_35715457

2017-02-12 11:50

http://ac.jobdu.com/problem.php?pid=1096

``````有两个日期，求两个日期之间的天数，如果两个日期是连续的我们规定他们之间的天数为两天
``````

``````有多组数据，每组数据有两行，分别表示两个日期，形式为YYYYMMDD
``````

``````每组数据输出一行，即日期差值
``````

``````20110412
20110422
``````

``````11
``````

`````` public static void main(String args[]) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
Date d1 = df.parse(cin.next());
Date d2 = df.parse(cin.next());
long res = (d2.getTime()-d1.getTime())/(24*3600*1000);
res = Math.abs(res)+1;
System.out.println(res);
}
}
``````
• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 1条回答

• 海之沐 4年前
`````` package com.study;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Test {
static final int ab = 4;

public static void main(String[] args) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
Date d1 = df.parse(cin.next());
Date d2 = df.parse(cin.next());
long res = (d2.getTime()-d1.getTime())/(24*3600*1000);
res = Math.abs(res)+1;
System.out.println(res);
}
}
}

``````

经测试，结果正确呀。看一下你导得包是否正确咯

点赞 评论 复制链接分享