aspose file tools*
The moose likes Java in General and the fly likes comparing date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparing date " Watch "comparing date " New topic
Author

comparing date

Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
Hi

i have to write a function which will build a SQL for a certain range of dates. But i am unable to compare the date

My code is

------------------------
public String loopDate(String d1,String d2) throws ParseException
{System.out.println(d1);
String dql = "select";
java.util.Date tdate ;
java.util.Date fdate;


tdate = new SimpleDateFormat("MM/dd/yy").parse(d1);
fdate = new SimpleDateFormat("MM/dd/yy").parse(d2);
//tdate = new SimpleDateFormat("yyyy-MM-dd").parse(d1);
String strOutDt = new SimpleDateFormat("MMM-yyyy").format(tdate);
System.out.println(tdate);
System.out.println("the date is"+ strOutDt);
// i want to run this loop while tdate<= fdate)
While (tdate <= fdate); // will throw an error
{
dql = dql + new SimpleDateFormat("MMM-yyyy").format(tdate);
}

return dql;

---------------------------------------------------------------

How do i compare the date in while
my dql = select Nov-2003 Dec-2003 .........................
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18876
    
    8

Let's assume your tdate and fdate variables are the Date variables and not the SimpleDateFormat variables of the same name. (Hint: copy and paste from real code.) Now, the Date class has before(Date) and after(Date) methods that you can use to compare two Dates, with the obvious meanings. So one of these two:depending on what you want to do with "equal" Dates.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

You can also use getTime() to get the long values of the dates and compare them. This has some very useful features and is a lot easier to work with than the Date/Calendar API. Only downside is the Year 2038 problem, although most people tend to ignore it.
[ December 19, 2005: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Off-topic:

Only downside is the Year 2038 problem, although most people tend to ignore it.

My undestranding is that since Java use 64 bits to store dates, it doesn't suffer from the 2038 problem (though I suppose it does suffer from the 292278994 problem). Is my understanding wrong?
[ December 19, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
hi,

thanks for the reply ( i should have seen the java docs)

is it possible in Date class to add a date that is if i want to increment the Date by one month untill it reached to a specific year

or do i have to use Calender class for it

like in VB we have "tempDt = DateAdd("m", 1, tempDt)"
this will add one month to the the presnet date
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

For adding/subtracting time units that involves months/years, the getTime() method will not suffice so it is recommended that you use the Calendar API such as:

Of course if you are doing any kind of loop you would want to increment the month while using myCalendar.get(Calendar.YEAR) to check the year.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing date