This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes compare year and date ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "compare year and date ??" Watch "compare year and date ??" New topic
Author

compare year and date ??

majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 164
Hi, I am using Mysql , and using DBtag so I can return the date from the database this form : 2000-10-13, I am interested to compare only the year and the month(2001 and 10 in this example) to the current year and month.
Pleas help.
thanks
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
First you have to create two date objects
one with the date of your database ( Date stored = someMethod() )
and other with the current date ( Date current = new Date() )
then you may use an instance of Calendar to get the year and the month and hold them in int variables say: cYear , cMonth ( to current year and month ) and sYear, sMonth ( for stored year and month )
Calendar c = new GregorianCalendar();
c.setTime( stored );
int sYear = c.get( Calendar.YEAR );
int sMonth = c.get( Calendar.MONTH );
c.setTime ( current );
int cYear = c.get( Calendar.YEAR );
int cMonth = c.get( Calendar.MONTH );
the way you get the stored date is up to you
if you need more help just post the sql so I can tell you how to retrive it and store it in a Date object
Hope it helps
Originally posted by majid nakit:
Hi, I am using Mysql , and using DBtag so I can return the date from the database this form : 2000-10-13, I am interested to compare only the year and the month(2001 and 10 in this example) to the current year and month.
Pleas help.
thanks

majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 164
Hi, and than you,
I did not understand what does stored in c.setTime( stored ); means.
By using a bean I have the curent Year and Month :
<jsp:useBean id='clock' scope='page' class='dates.JspCalendar' type="dates.JspCalendar" />
Year: is <jsp:getProperty name="clock" property="year"/>
Month: is <jsp:getProperty name="clock" property="month"/>
But the date that I got back from the databse is a string like this : "2001-05-01"
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Your post is a bit confusing.

Are you using the bean, and getting back the information you want? That is: Does the bean give you the month and year when you use them like you've shown?

Then in a separate process or part of the JSP, you want to query a database, which returns the date as a string of the form "2001-05-01", and you want to compare this, to the month and year returned by the bean tags?

You say "But the date I got back from the database ..." but where is the code that shows us how you get this?

As for what c.setTime(stored) means... Zkr was showing you how to use an instance of the GregorianCalendar class (c) and an instance of the Date class (stored) to retrieve only the month and year.. which you have said is what you needed.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: compare year and date ??
 
Similar Threads
Making String to java.util.Date
Problems in converting to a Date Object.
compare year and month ???
Problems in converting to a Date Object.
compare year and month ???