Java Compare Date Example

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

1 . srcDate.compareTo(destDate) : You can use this method to compare two Date object directly. If return 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) : All this methods return a boolean value. Return true means srcDate equal, before or after destDate, vice versa.

	/* 
	 * 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() : This method can not compare two date 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 number of days between two Date object.
Below example just use 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.

	/* 
	 * 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.

	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);
	}

Output of above java compare date example code.java compare date example output

Download “JavaCompareDateExample.zip” JavaCompareDateExample.zip – Downloaded 58 times – 1 KB

(Visited 1,056 times, 1 visits today)
READ :   How to fix java Unsupported major.minor version 51.0 Exception

Leave a Reply

Your email address will not be published. Required fields are marked *

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