SoFunction
Updated on 2025-03-02

Examples of 4 simple methods for calculating the number of days of date difference in java

Method 1: Subtract long values ​​(recommended)

public static void main(String[] args) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 try {
		        Date startDate = ("2024-03-01 10:00:00");//Start time		        Date endDate = ("2024-03-02 14:00:00");//End time		        long msNum = ()-();//The number of milliseconds of the timestamp difference                long dayNum = msNum/(24*60*60*1000)//Divide by the milliseconds of a day to get the difference of days		        ("The difference of days is:"+ dayNum);
		 } catch (ParseException e) {
		        ();
		 }
}

Method 2:

public static void main(String[] args) {
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
	    try {
	        Date star = ("2020-02-03");//Start time	        Date endDay=("2025-03-02");//End time	        Date nextDay=star;
	        int i=0;
	        while((endDay)){// When tomorrow does not end time, it is to terminate the loop	        	Calendar cld = ();
	 	        (star);
	 	        (, 1);
	 	        star = ();
	 	        //Get the next day date string	 	        nextDay = star; 
	 	        i++;
	        }
	       ("The difference of days is:"+i);
	    } catch (ParseException e) {
	        ();
	    }
	}

Method 3:

public static void main(String[] args) {
		String star="2020-02-03";
		String end="2025-03-02";
		String[] star1=("-");
		String[] end1=("-");
		int days=0;
		if((star1[0])<(end1[0])){
			for(int i=(star1[0]);i<(end1[0]);i++){
				//Calculate whether it is Ruinian				if(i%4==0&&i%100!=0||i%400==0){
					days+=366;
				}else{
					days+=365;
				}
			}
		}
		//Get the date that has passed since the beginning of the year		int starday=days(star1[0],star1[1],star1[2]);
		//Get the date that ended that year has passed		int endday=days(end1[0],end1[1],end1[2]);
		//Subtract the date that has passed since the beginning of the year, plus the date that has passed when the end of the year.		days=days-starday+endday;
		("Date of difference:"+days);
	}
	public static int days(String year,String month,String day){
		int days=0;
		int nowyear=(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<(month);i++){
			if(flag){
				days+=monthday1[i];
			}else{
				days+=monthday[i];
			}
		}
		days+=(day);
		return days;
	}

Method 4:

int y;
	int m;
	int d;

	public test2(int y,int m,int d ){
		=y;
		=m;
		=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<;i++){
			if(i%4==0&& i%100!=0 || i%400==0){
				day+=366;
			}else{
				day+=365;
			}
		}
		if(%4==0&& %100!=0 || %400==0){
			x[2]=29;
		}
		for(int i=1;i<;i++){

			day+=x[i];	
		}
		day+=;
		(day);
		return day;

	}
	public int DiffDays(test2 d){//Member method for calculating the number of days apart between two dates		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); 
			("Date of difference:"+(d2)); 
		}catch(Exception e){ 
			("error"); 
		} 
	}

Attachment: Calculate the number of days between the two dates (excluding weekends)

First of all, we can simplify the problem. We can know that no matter the start or end date, as long as 7 days pass, two days will definitely be weekends. Therefore, the problem can be simplified to the number of weekend days + weeks *2 in a week.

public int checkWeekendDay(int startDate, int endDate, int days){
        int weekEndCount = 0;
        if (days<1){
            if (startDate==6 || startDate==0){
                weekEndCount+=1;
            }
        } else {
            weekEndCount+=(days/7)*2;
            weekEndCount+=calculateWeekendDays(startDate,endDate);
        }

        return weekEndCount;
    }

public int calculateWeekendDays(int startDate, int endDate){
        int weekendCount=0;
        if (startDate==0&&endDate==6) {
            weekendCount += 2;
        }else if (startDate<endDate&&endDate==6){
            weekendCount+=1;
        }else if (startDate<endDate&&startDate==0){
            weekendCount+=1;
        }else if (startDate>endDate){
            weekendCount+=2;
        }
        if (startDate==endDate){
            if (startDate==6||endDate==0){
                weekendCount+=1;
            }
        }

        return weekendCount;
    }

Summarize

This is the article about 4 simple methods for calculating date difference days in Java. For more related content on calculating date difference days in Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!