• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to compare jstl value with String value

 
Sameer Nandgaonkar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my code i am using jstl tags.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
GregorianCalendar cal = new GregorianCalendar();
String dd = sdf.format(cal.getTime());
Here i can get current date which is in (2007-05-16)Format.
<c:if test="${row.followup_date==currdate_id}">//Here i am comparing followup_date with my current date.The problem is my followup_date is in date format(YYYY-MM-DD).I am comparing this value but it will not work.Can you please tell me how can i access that value which is dd in this c:if tag. please help me.Instead of currdate_id in c:if i use 2000-05-16 hard coded the code work properly.but when i compare with the variable it won't work.please tell me.
 
Manish Nakhre
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you using dd in your code? Shouldn't it be:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
GregorianCalendar cal = new GregorianCalendar();
String dd = sdf.format(cal.getTime());
<c:if test="${row.followup_date==dd}">/

If this does not work, try this:

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
String dd = sdf.format(date);
<c:if test="${row.followup_date==dd}">/
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming row.followup_date is a String in the format "yyyy-MM-dd"
I'm a big fan of eliminating scriptlet code, so here is the JSTL equivalent.



Note: don't mix up scriptlet variables with EL variables as the previous poster (Manish Nakhre) seems to have done. They are not the same thing at all.

Personally I also thing that row.followup_date should be a Date object, and you should format it when you want to display it, not before. Which would make the comparision between Dates rather than formatted strings.

Cheers,
evnafets
 
Manish Nakhre
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephen. Your code looks much cleaner. I am a newbie on JSTL, so can you tell me how JSTL understands that currentDate is a new java.util.Date?

When you specify the classname as type="java.util.Date" in
<jsp:useBean id="currentDate" type="java.util.Date"/>,
Does it call the constructor of java.util.Date and assigns it to currentDate?

Sameer, did you try this code?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Becasue the type attribute is used, this action will simply take whatever scoped variable is already defined as currentDate and assign it to a scripting variable named currentDate. This is not a very useful thing to do when using the EL and JSTL.

I'm pretty sure Stefan meant:



Note the use of class rather than type. This will do the same as the above except that it will create currentDate as a scoped variable if it doesn't already exist (using the nullary constructor). This side effect is what is useful to the JSTL and EL. Obvsiouly the created scripting vairable is never used.
[ August 15, 2007: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!