Four ways to calculate the number of days between two dates in java

Four ways to calculate the difference in days between two dates
The first method: the timestamp method, calculate the difference between the timestamps of the two dates, and then divide by the number of milliseconds of the day to get the difference in days.

public static void main(String[] args) {
    
    
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		 try {
    
    
		        Date star = dft.parse("2020-02-03");//开始时间
		        Date endDay=dft.parse("2025-03-02");//结束时间
		        Long starTime=star.getTime();
		        Long endTime=endDay.getTime();
		        Long num=endTime-starTime;//时间戳相差的毫秒数
		        System.out.println("相差天数为:"+num/24/60/60/1000);//除以一天的毫秒数
		 } catch (ParseException e) {
    
    
		        e.printStackTrace();
		    }
	}

The second method: try the Calendar class of the util package, add one day each time, and know when it is equal to the end time.

public static void main(String[] args) {
    
    
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
	    try {
    
    
	        Date star = dft.parse("2020-02-03");//开始时间
	        Date endDay=dft.parse("2025-03-02");//结束时间
	        Date nextDay=star;
	        int i=0;
	        while(nextDay.before(endDay)){
    
    //当明天不在结束时间之前是终止循环
	        	Calendar cld = Calendar.getInstance();
	 	        cld.setTime(star);
	 	        cld.add(Calendar.DATE, 1);
	 	        star = cld.getTime();
	 	        //获得下一天日期字符串
	 	        nextDay = star; 
	 	        i++;
	        }
	       System.out.println("相差天数为:"+i);
	    } catch (ParseException e) {
    
    
	        e.printStackTrace();
	    }
	}

The third type: the start and end time is divided into year, month and day, and the number of days in each year and the number of days in month and day are calculated cyclically, and logically added and subtracted.

public static void main(String[] args) {
    
    
		String star="2020-02-03";
		String end="2025-03-02";
		String[] star1=star.split("-");
		String[] end1=end.split("-");
		int days=0;
		if(Integer.parseInt(star1[0])<Integer.parseInt(end1[0])){
    
    
			for(int i=Integer.parseInt(star1[0]);i<Integer.parseInt(end1[0]);i++){
    
    
				//计算是否是瑞年
				if(i%4==0&&i%100!=0||i%400==0){
    
    
					days+=366;
				}else{
    
    
					days+=365;
				}
			}
		}
		//得到开始那一年已过去的日期
		int starday=days(star1[0],star1[1],star1[2]);
		//得到结束那一年已过去的日期
		int endday=days(end1[0],end1[1],end1[2]);
		//减去开始那一年已过去的日期,加上结束那一年已过去的日期
		days=days-starday+endday;
		System.out.println("相差的天数:"+days);
	}
	public static int days(String year,String month,String day){
    
    
		int days=0;
		int nowyear=Integer.parseInt(year);
		int[] monthday={
    
    0,31,28,31,30,31,30,31,31,30,31,30,31};
		int[] monthday1={
    
    0,31,29,31,30,31,30,31,31,30,31,30,31};
		boolean flag=true;
		if(nowyear%4==0&&nowyear%100!=0||nowyear%400==0){
    
    
		}else{
    
    
			flag=false;
		}
		for(int i=0;i<Integer.parseInt(month);i++){
    
    
			if(flag){
    
    
				days+=monthday1[i];
			}else{
    
    
				days+=monthday[i];
			}
		}
		days+=Integer.parseInt(day);
		return days;
	}

The fourth method: calculate the total number of days from a given date to 0001-01-01 in the form of a class, and then make a difference between the days of the two dates to obtain the difference in days.

int y;
	int m;
	int d;

	public test2(int y,int m,int d ){
    
    
		this.y=y;
		this.m=m;
		this.d=d;
	}
	public int sum(test2 d){
    
    
		int day=0;
		int[] x={
    
    0,31,28,31,30,31,30,31,31,30,31,30,31};
		for(int i=1;i<d.y;i++){
    
    
			if(i%4==0&& i%100!=0 || i%400==0){
    
    
				day+=366;
			}else{
    
    
				day+=365;
			}
		}
		if(d.y%4==0&& d.y%100!=0 || d.y%400==0){
    
    
			x[2]=29;
		}
		for(int i=1;i<d.m;i++){
    
    

			day+=x[i];	
		}
		day+=d.d;
		System.out.println(day);
		return day;

	}
	public int DiffDays(test2 d){
    
    //计算两个日期之间的相距天数的成员方法 
		int s1=sum(this);
		int s2=sum(d);
		if(s1>s2){
    
    
			return s1-s2;
		}else{
    
    
			return s2-s1;
		}
	}
	public static void main(String args[]){
    
     
		int a,b,c; 
		test2 d1,d2; 
		try{
    
     
			d1=new test2(2020,02,03); 
			d2=new test2(2025,03,02); 
			System.out.println("相差的天数:"+d1.DiffDays(d2)); 
		}catch(Exception e){
    
     
			System.out.println("error"); 
		} 
	}

Guess you like

Origin blog.csdn.net/qq_44543774/article/details/131592516