Java 使用Date方法统计天数

import java.text.ParseException;
import java.text.SimpleDateFormat;  
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
String d1 = "1900-01-01";
String d2 = "2017-12-31";
String d3 = "2018-3-31";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format.parse(d1);
Date date2 = format.parse(d2);
Date date3 = format.parse(d3);
int a = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
int b = (int) ((date3.getTime() - date1.getTime()) / (1000*3600*24));
System.out.println(a+1);
System.out.println(b+1);


}
}

你可能感兴趣的:(Java)