Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare year and date ??

 
majid nakit
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic