• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Compare date using Java

kmthien

Senior member
Hi,

I got the following code :

String str1 = "01/17/2001";
String str2 = "2001-01-17";
SimpleDateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null, date2 = null;
try {
date1 = df1.parse(str1);
} catch (java.text.ParseException e) {
// handle invalid date
}
try {
date2 = df2.parse(str2);
} catch (java.text.ParseException e) {
// handle invalid date
}

How to compare if the different of the 2 Date objects is 2 months or 2 years ? Like

if((date1 - date2) == 2 months)
System.out.println("2 months different");

Pls help.......
 
use the calendar object's "add" and "before" or "after" methods.
ie
//gets current date/time
Calendar calNow = Calendar.getInstance();
//gets date you want to compare to. In this case it's 4 months before today
Calendar calCompareTo = Calendar.getInstance().add(Calendar.MONTH,-4);

//compare calendar to
if ( cal.getTime().before(calCompareTo) )
//success
 
Hi AmigaMan,

I got the following errors after compiling your code :

incompatible types

required : java.Util.Calendar
Calendar calCompareTo = Calendar.getInstance().add(Calendar.MONTH,-4);


before(java.util.Date) in java.util.Date cannot be applied to (java.util.Calendar)

if (cal.getTime().before(calCompareTo))




The code is as below:

import java.util.*;
import java.awt.*;
import java.text.*;


public class DateCompare
{
public static void main(String[] pArgs)
{
String str1 = "01/17/2001";

SimpleDateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");

Date date1 = null;
try {
date1 = df1.parse(str1);
} catch (java.text.ParseException e) {
// handle invalid date
}
Calendar calNow = Calendar.getInstance();
//gets date you want to compare to. In this case it's 4 months before today
Calendar calCompareTo = Calendar.getInstance().add(Calendar.MONTH,-4);
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
//compare calendar to
if ( cal.getTime().before(calCompareTo) )
{
System.out.println("4 months");
}
}
}
 
Get an instance of calendar. Set it to whatever you want your comparison to be. Create a date object. Use the calendars before or after methods against the date object to do the comparison. You're right though, my code doesn't work. But I'm not going to do your work for you.
 
Yeah, I got the code work as follow:

public class DateCompare
{
public static void main(String[] pArgs)
{
String str1 = "04/13/2003";
String str2 = "2001-01-17";
SimpleDateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null, date2 = null;

try {
date1 = df1.parse(str1);
} catch (java.text.ParseException e) {
// handle invalid date
}
try {
date2 = df2.parse(str2);
} catch (java.text.ParseException e) {
// handle invalid date
}

Calendar calNow = Calendar.getInstance();
//gets date you want to compare to. In this case it's 4 months before today
Calendar calCompareTo = Calendar.getInstance();
calCompareTo.add(Calendar.DATE,-1);
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
//compare calendar to
date2 = cal.getTime();
date1 = calCompareTo.getTime();
System.out.println(date1.toString());
if(date2.before(date1))
{
System.out.println("Thanks");
}
}
}
 
Back
Top