This week's book giveaway is in the Cloud/Virtualizaton forum.We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Getting the maximum of two dates and date comparison

ramya narayanan
Ranch Hand
Posts: 338
1)Is it possible to know the maximum of two dates?
Suppose
maximum(nov 11,2008,nov 13,2008) -> nov 13,2008.
Is there any built in method for it?

For comparing two integers there is a maximum method in Collections class?
Collections.maximum(11,13)

Like wise any built-in method to calculate the maximum of two dates?

2) Can we use the relational operators(like <= , >=) on these dates.

Regards.

Joanne Neal
Rancher
Posts: 3742
16
Have a look at the java.util.Date class. There are three methods in there that you could make use of.

ramya narayanan
Ranch Hand
Posts: 338
I think so that you are saying about the before and after method.
But this does only
Tests if this date is before the specified date.(before method)
or
Tests if this date is after the specified date(after method)

But I want to
1) find the maximum of two dates & to display the maximum date
2) To conditionally check whether one date is <= other date

Regards.

Joanne Neal
Rancher
Posts: 3742
16
As far as I know, there's no method that takes two dates and returns the maximum one (I presume you mean the later one), but it wouldn't be difficult to write such a method using the before, after or compareTo methods of the Date class.

Ranch Hand
Posts: 103
Hi Ranya,

These methods(before and after) will also do the same checking. right?

If dt1 and dt2 are two date objects,
dt1.before(dt2)
will give true if, dt1 is prior to dt2(ie, dt1<dt2).
after() will do the exact opposite thing.

The compareTo() is working just like the objects comarison in collections(will return -1,0 or 1) according to the condition.

ramya narayanan
Ranch Hand
Posts: 338
I've just found it out.

The thing is both Date and String objects are Comparable & so it can be compared & maximum value found out.

The complete code.

import java.util.Date;

public class DateCheck
{
public static void main(String[] args)
{
Date d1=new Date();
Date d2=new Date();
//passing date instances to an method which returns the max date
Date maxDate=Maxdate.max(d1,d2);
System.out.println("Maximum date is :"+maxDate);
}
}

import java.util.*;
public class Maxdate
{
public static Comparable max(Comparable c1,Comparable c2)
{
if(c1.compareTo(c2) >0)
return c1;
else
return c2;
}
}

But now my doubt is how to use relational operators like <= or >= date instances.
Can i use like this

if(d1<=d2)

Regards.

ramya narayanan
Ranch Hand
Posts: 338
I think so it will work.

//if first date is lesser or equal to the second date
if(d1.compareTo(d2)<=0)
{
}

Please correct me if i'm wrong.
Regards.

Joanne Neal
Rancher
Posts: 3742
16
Originally posted by ramya narayanan:
But now my doubt is how to use relational operators like <= or >= date instances.

You can't. Unlike C++ you can't overload arithmetic operators.

ramya narayanan
Ranch Hand
Posts: 338
Yeah you're right joanne.
I'm not using relational operators on instances d1 & d2, but i'm comparing them using the compareTo method.
if(d1.compareTo(d2) <=0)

Regards.

Campbell Ritchie
Sheriff
Posts: 48952
60
Please use code tags for code, not the quote button.

ramya narayanan
Ranch Hand
Posts: 338
For implementing relational operators like <= on the date instances on the context that date instances d1,d2 are Comparable, whether it will work.

Regards.
Ignorance is a bliss

Joanne Neal
Rancher
Posts: 3742
16
Originally posted by ramya narayanan:
For implementing relational operators like <= on the date instances on the context that date instances d1,d2 are Comparable, whether it will work.

Regards.
Ignorance is a bliss

That code will work, but it is not implementing relational operators on the date instances. The <= is comparing two ints - the one returned from the compareTo method and 0.