获取两个时间段相差天数


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ManyDays {
public static void main(String[] args) throws ParseException {
SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");//定义新的时间格式,方便后面解析
Date date = new Date();//date数据
String format = ss.format(date);//format将现在的日期转换为string
Date begin = ss.parse("2022-02-20");//simpledateformat中的parse方法解析string,需要此前定义格式
Date end = ss.parse(format);//simpledateformat中的parse方法解析string,需要此前定义格式
Long endTime = end.getTime();//dategettime方法,可以返回毫秒数(从1970年开始到时间点)
Long beginTime = begin.getTime();//dategettime方法,可以返回毫秒数(从1970年开始到时间点)
Long mathTime = endTime - beginTime;
System.out.println("共计过去:" + mathTime / 1000 / 60 / 60 / 24 + ".");
}
}
posted @ 2022-03-02 19:27  WhileGo  阅读(95)  评论(0)    收藏  举报