Java Compare Date Example

The class java.util.Date provides a lot of methods that can be used to compare two Date objects. They are compareTo(), equals(), before(), after() and getTime(). This article will show you examples of how to use them to compare java Date objects.

1. srcDate.compareTo(destDate).

  1. You can use this method to compare two Date objects directly.
  2. If the method returns 0 means srcDate is equal to destDate, return 1 means srcDate is after destDate, return -1 means srcDate is before destDate.
    /* 
     * Use compareTo() method to compare.
     * */
    public static int copareDateUseCompareTo(Date srcDate, Date destDate)
    {
    	int ret = 0;
    	ret = srcDate.compareTo(destDate);
    	
    	JavaCompareDateExample.printDateCompareInfo(srcDate, destDate, ret);
    	return ret;
    }

2. srcDate.equals(destDate), srcDate.before(destDate), srcDate.after(destDate).

  1. All these methods return a boolean value.
  2. Return true means srcDate equal, before or after destDate, return false means srcDate not equal, after or equal, before or equal destDate.
    /* 
     * Use equals(), before() and after() method to compare.
     * */
    public static int copareDateUseDateMethod(Date srcDate, Date destDate)
    {
    	int ret = 0;
    		
    	if(srcDate.equals(destDate))
    	{
    		ret = 0;
    	}else if(srcDate.before(destDate))
    	{
    		ret = -1;
    	}else if(srcDate.after(destDate))
    	{
    		ret = 1;
    	}
    		
    	JavaCompareDateExample.printDateCompareInfo(srcDate, destDate, ret);
    	
    	return ret;
    }

3. srcDate.getTime().

  1. This method can not compare two dates directly. But we can use it to get the compared date object’s number of milliseconds from 1970/01/01, then use that value to compare.
    /* 
     * Use getTime() method to compare.
     * */
    public static int copareDateUseGetTime(Date srcDate, Date destDate)
    {
    	int ret = 0;
    	
    	long srcTime = srcDate.getTime();
    	
    	long destTime = destDate.getTime();
    	
    	long deltaTime = srcTime - destTime;
    	
    	if(deltaTime==0)
    	{
    		ret = 0;
    	}else if(deltaTime > 0)
    	{
    		ret = 1;
    	}else if(deltaTime < 0)
    	{
    		ret = -1;
    	}
    		
    	JavaCompareDateExample.printDateCompareInfo(srcDate, destDate, ret);
    	
    	return ret;
    }

4. Get The Number Of Days Between Two Date Objects.

  1. Below example just use the getTime() method to get the number of days between srcDate and destDate.
    	/* 
    	 * Get days number between two date.
    	 * */
    	public static double getDaysBetweenTwoDate(Date srcDate, Date destDate)
    	{	
    		long startTime = srcDate.getTime();
    		
    		long destTime = destDate.getTime();
    		
    		long deltaTime = destTime - startTime;
    		
    		long oneDayTime = 24 * 60 * 60 * 1000;
    		
    		double deltaDay = Math.abs(deltaTime / oneDayTime);
    		
    		System.out.println(deltaDay + " days between " + JavaCompareDateExample.getDateStringValue(srcDate) + " and " + JavaCompareDateExample.getDateStringValue(destDate));
    		
    		return deltaDay;
    	}

5. Below Are Some Util Methods Used In This Example. You Can See The Comments For Detail Explanation.

  1. The util methods in this example.
    	/* 
    	 * Return Date object based on the input string and its's format.
    	 * */
    	public static Date getDateByDateFormat(String dateStr, String dateStrFormat)
    	{
    		Date ret = new Date();
    		
    		try
    		{
    			if(dateStr!=null && dateStrFormat!=null)
    			{
    				SimpleDateFormat dateFormat = new SimpleDateFormat(dateStrFormat); 
    				ret = dateFormat.parse(dateStr);
    			}
    		}catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}finally
    		{
    			return ret;
    		}
    	}
    	
    	/* 
    	 * Get srcDate's string value use standard format.
    	 * */
    	public static String getDateStringValue(Date srcDate)
    	{
    		String ret = "";
    		
    		try
    		{
    			if(srcDate!=null)
    			{
    				SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy"); 
    				ret = dateFormat.format(srcDate);
    			}
    		}catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}finally
    		{
    			return ret;
    		}
    	}
    	
    	/* 
    	 * Print out the comapre result imformation.
    	 * */
    	public static void printDateCompareInfo(Date srcDate, Date destDate, int compareResult)
    	{
    		if(compareResult==0)
    		{
    			System.out.println(JavaCompareDateExample.getDateStringValue(srcDate) + " is equals to " + JavaCompareDateExample.getDateStringValue(destDate));
    		}else if(compareResult>0)
    		{
    			System.out.println(JavaCompareDateExample.getDateStringValue(srcDate) + " is after " + JavaCompareDateExample.getDateStringValue(destDate));
    		}else if(compareResult<0)
    		{
    			System.out.println(JavaCompareDateExample.getDateStringValue(srcDate) + " is before " + JavaCompareDateExample.getDateStringValue(destDate));
    		}
    	}

6. Below Is The Main Method That Run Above Compare Methods.

  1. The example’s main method.
    	public static void main(String[] args) {
    		
    		// Create three pair object that need to compare.
    		Date srcDate = JavaCompareDateExample.getDateByDateFormat("08/01/2017", "MM/dd/yyyy");
    		Date destDate = JavaCompareDateExample.getDateByDateFormat("09/01/2017", "dd/MM/yyyy");
    		
    		Date srcDate1 = JavaCompareDateExample.getDateByDateFormat("08/01/2017", "MM/dd/yyyy");
    		Date destDate1 = JavaCompareDateExample.getDateByDateFormat("08/01/2017", "MM/dd/yyyy");
    		
    		Date srcDate2 = JavaCompareDateExample.getDateByDateFormat("2017-07-01", "yyyy-MM-dd");
    		Date destDate2 = JavaCompareDateExample.getDateByDateFormat("2017-07-02", "yyyy-MM-dd");
    
    		// Use compareTo() to compare.
    		System.out.println("Compare use compareTo.");
    		JavaCompareDateExample.copareDateUseCompareTo(srcDate, destDate);
    		JavaCompareDateExample.copareDateUseCompareTo(srcDate1, destDate1);
    		JavaCompareDateExample.copareDateUseCompareTo(srcDate2, destDate2);
    		
    		// Use getTime() to compare.
    		System.out.println("");
    		System.out.println("Compare use getTime.");
    		JavaCompareDateExample.copareDateUseGetTime(srcDate, destDate);
    		JavaCompareDateExample.copareDateUseGetTime(srcDate1, destDate1);
    		JavaCompareDateExample.copareDateUseGetTime(srcDate2, destDate2);
    		
    		// Use equals(), before() and after() methods to compare.
    		System.out.println("");
    		System.out.println("Compare use Date methods.");
    		JavaCompareDateExample.copareDateUseDateMethod(srcDate, destDate);
    		JavaCompareDateExample.copareDateUseDateMethod(srcDate1, destDate1);
    		JavaCompareDateExample.copareDateUseDateMethod(srcDate2, destDate2);
    		
    		// Calculate the days between.
    		System.out.println("");
    		System.out.println("Calculate between days number.");
    		JavaCompareDateExample.getDaysBetweenTwoDate(srcDate, destDate);
    		JavaCompareDateExample.getDaysBetweenTwoDate(srcDate1, destDate1);
    		JavaCompareDateExample.getDaysBetweenTwoDate(srcDate2, destDate2);
    	}
  2. The output of the above java compares dates example.
    Start date compare use compareTo.
    Aug 01 2017 is after Jan 09 2017
    Aug 01 2017 is equals to Aug 01 2017
    Jul 01 2017 is before Jul 02 2017
    
    Start date compare use getTime.
    Aug 01 2017 is after Jan 09 2017
    Aug 01 2017 is equals to Aug 01 2017
    Jul 01 2017 is before Jul 02 2017
    
    Start date compare use Date methods.
    Aug 01 2017 is after Jan 09 2017
    Aug 01 2017 is equals to Aug 01 2017
    Jul 01 2017 is before Jul 02 2017
    
    Start days between two date calculate.
    204.0 days between Aug 01 2017 and Jan 09 2017
    0.0 days between Aug 01 2017 and Aug 01 2017
    1.0 days between Jul 01 2017 and Jul 02 2017
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x