Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the maximum of two dates and date comparison

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags for code, not the quote button.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic