• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

comparing date

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .........................
 
Sheriff
Posts: 28414
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 4356
45
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4356
45
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic