• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing date

 
Raj kalaria
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Raj kalaria
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic