Compare date using Java

kmthien

Senior member
Oct 8, 2002
363
0
0
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.......
 

AmigaMan

Diamond Member
Oct 12, 1999
3,644
1
0
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
 

kmthien

Senior member
Oct 8, 2002
363
0
0
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");
}
}
}
 

AmigaMan

Diamond Member
Oct 12, 1999
3,644
1
0
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.
 

kmthien

Senior member
Oct 8, 2002
363
0
0
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");
}
}
}