0

I've found out a very useful library which is the SimpleDateFormat for the formatting of date. That is not the problem. The compareTo method serve my purpose of comparing the date but my problem is that the compareTo doesn't show the difference between then

For example the code below:

    String date1 = "Apr 2010";
    String date2 = "Jan 2009";

    SimpleDateFormat format = new SimpleDateFormat("MMM yy");

    Date d1 = null;
    Date d2 = null;
    d1 = format.parse(date1);
    d2 = format.parse(date2);

    int test1 = d1.compareTo(d2);
    System.out.println(test1);

The output of the above gives me -9. May I know how does the method compareTo calculates it?

What I want is that to find the difference between those months and years. Thank you very much.

user3054491
  • 51
  • 1
  • 7

3 Answers3

4

The compareTo method is inherited from Comparable. Its javadoc states

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

So that is all you are going to get from compareTo. Just which is bigger/smaller/equal.

If you want to compare date fields, you will need to do it yourself. Or use a third party library like Joda Time. For example

String date1 = "Apr 2010";
String date2 = "Jan 2009";

DateTimeFormatter formatter = DateTimeFormat.forPattern("MMM yyyy");

DateTime d1 = formatter.parseDateTime(date1);
DateTime d2 = formatter.parseDateTime(date2);

Months monthsBetween = Months.monthsBetween(d1, d2);
System.out.println("Months diff: " + monthsBetween.get(DurationFieldType.months()));
Years yearsBetween = Years.yearsBetween(d1, d2);
System.out.println("Years diff: " + yearsBetween.get(DurationFieldType.years()));

prints

Months diff: -15
Years diff: -1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I see, I thought it would return values for the difference of dates. Thank you very much for your information. – user3054491 Dec 12 '13 at 02:08
  • @user3054491 You're welcome. The support for Date object is going to greatly improve in Java 8, so be on the lookout. – Sotirios Delimanolis Dec 12 '13 at 02:13
  • 1
    Oh my, Thank you for the information. You've been a great help to me ! – user3054491 Dec 12 '13 at 02:13
  • One question, I copied and paste your code into my NetBeans but I got error on the `Month`. Is Month a library? Because NetBeans doesn't show what library to include and I'm getting the error there. – user3054491 Dec 12 '13 at 02:18
  • @user3054491 Yes, that's what I meant by 3rd party library. Joda Time is a library specialized in date processing. You can check out the project [here](http://www.joda.org/joda-time/). You can download the version you want [here](http://sourceforge.net/projects/joda-time/files/joda-time/). You will need to add them to your Netbeans project build path. – Sotirios Delimanolis Dec 12 '13 at 02:20
  • Oh, thanks alot, you've really been a great helper here ! – user3054491 Dec 12 '13 at 02:21
0

Check out the Calandar class. it's used to get and set actual date properties like years, months, days etc. here is one I wrote recently...

public Date addDays(Date date, int deltaDays) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date.getTime());
    calendar.add(Calendar.DAY_OF_MONTH, deltaDays);
    return new Date( calendar.getTimeInMillis() );
}

public Date getFirstWeekdayOfYear(int dayOfWeek, int year) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.MONTH, 0);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    while (calendar.get(Calendar.DAY_OF_WEEK) != dayOfWeek) {
        calendar.add(Calendar.DATE, 1);
    }
    return new Date( calendar.getTimeInMillis());
}

public boolean isOlderThan(Date date, int days) {
    return date.getTime() < getToday(0-days).getTime();
}
slipperyseal
  • 2,728
  • 1
  • 13
  • 15
0

You need to make Calendars of Dates

Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = ...
...

then get years and months from Calendars

int y1= c1.get(Calendar.YEAR);
int m1 = c1.get(Calendar.MONTH);
int y2 = 
...

now find the difference using integer arithmetic

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275