aspose file tools*
The moose likes Beginning Java and the fly likes Getting the maximum of two dates and date comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting the maximum of two dates and date comparison" Watch "Getting the maximum of two dates and date comparison" New topic
Author

Getting the maximum of two dates and date comparison

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
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.

Please reply?
Regards.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Have a look at the java.util.Date class. There are three methods in there that you could make use of.


Joanne
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
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.


Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
I've just found it out.
Thanks Joanne for your opinions.

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

Joined: Oct 06, 2008
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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

Joined: Oct 06, 2008
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
Please use code tags for code, not the quote button.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the maximum of two dates and date comparison
 
Similar Threads
FREE CERTIFICATION BETA: SCDJWS 5
Error listenerStart : Using spring with tomcat
Locale specific dates in struts2
how to find how many sundays comes in 2 Dates
Date difference