?
Solved

Date manipulation in a JSP

Posted on 2003-02-27
23
Medium Priority
?
387 Views
Last Modified: 2007-12-19
I am trying to get the previous date into a variable. can anyone guide me pl?

********
<% String ZDATE = ISR.getValue("EFF_DATE"); %>
<% String ZDATE = "ZDATE__" - 1; %>
******

thanks,

dattis
0
Comment
Question by:dattis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 10
23 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 8035931
try this:
<% String ZDATE = ISR.getValue("EFF_DATE");
java.text.DateFormat df = java.text.DateFormat.getDateInstance();

java.util.Calendar myDate = java.util.Calendar.getInstance();
myDate.setTime(df.parse(ZDATE));
myDate.add(Calendar.DATE, -1); // subtract one day
%>
<%=myDate%>

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8035937
Use DateFormat:
http://java.sun.com/j2se/1.3/docs/api/java/text/DateFormat.html

To format the date as you need.

CJ
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8036061
what's you date sting looks like? 12/31/2002?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dattis
ID: 8036097
it didn't work..
do I have to include any jav.util?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036108
what is your error?

importing java.util.* won't hurt.. but post your current code and the error you are getting.

CJ
0
 

Author Comment

by:dattis
ID: 8036116
the date string is in MM/DD/YYYY format
0
 

Author Comment

by:dattis
ID: 8036119
the date string is in MM/DD/YYYY format
0
 

Author Comment

by:dattis
ID: 8036135
<% String ZDATE = ISR.getValue("EFF_DATE"); %>
ava.text.DateFormat df=java.text.DateFormat.getDateInstance();
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(df.parse(ZDATE));
myDate.add(Calendar.DATE,-1);
          %>
         <td>
        <hbj:textView id="PAYDT" text='<%= myDate %>' />
        </td>
0
 

Author Comment

by:dattis
ID: 8036136
<% String ZDATE = ISR.getValue("EFF_DATE"); %>
ava.text.DateFormat df=java.text.DateFormat.getDateInstance();
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(df.parse(ZDATE));
myDate.add(Calendar.DATE,-1);
          %>
         <td>
        <hbj:textView id="PAYDT" text='<%= myDate %>' />
        </td>
0
 

Author Comment

by:dattis
ID: 8036138
<% String ZDATE = ISR.getValue("EFF_DATE"); %>
ava.text.DateFormat df=java.text.DateFormat.getDateInstance();
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(df.parse(ZDATE));
myDate.add(Calendar.DATE,-1);
          %>
         <td>
        <hbj:textView id="PAYDT" text='<%= myDate %>' />
        </td>
0
 

Author Comment

by:dattis
ID: 8036188
this is the error

Incompatible type for method. Can't convert java.util.Calendar to java.lang.String.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036205
so the problem is not the issue that it isn't working, it seems that it just can't display it using the tag you have

CJ
0
 

Author Comment

by:dattis
ID: 8036215
another error
Original exception: java.text.ParseException: Unparseable date: "02/27/2003"
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036233
try this:

<% String ZDATE = ISR.getValue("EFF_DATE"); %>
ava.text.DateFormat df=java.text.DateFormat.getDateInstance(MEDIUM);
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(df.parse(ZDATE));
myDate.add(Calendar.DATE,-1);
         %>
        <td>
       <hbj:textView id="PAYDT" text='<%java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM).format(myDate.getTime())%>' />
       </td>

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036281
for the parseException now use this:
<% String ZDATE = ISR.getValue("EFF_DATE"); %>
<% java.text.SimpleDateFormat formatter
= new java.text.SimpleDateFormat("MM/dd/yyyy");
ParsePosition pos = new ParsePosition(0);
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(formatter.parse(ZDATE,pos););
myDate.add(Calendar.DATE,-1);
        %>
       <td>
      <hbj:textView id="PAYDT" text='<%java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM).format(myDate.getTime())%>' />
      </td>

HTH,
CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036298
oops try this:
<% String ZDATE = ISR.getValue("EFF_DATE"); %>
<% java.text.SimpleDateFormat formatter
= new java.text.SimpleDateFormat("MM/dd/yyyy");
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(formatter.parse(ZDATE));
myDate.add(Calendar.DATE,-1);
       %>
      <td>
     <hbj:textView id="PAYDT" text='<%java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM).format(myDate.getTime())%>' />
     </td>

I don't think you need ParsePosition in this case.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036307
if you want to format the date as the same way try this:

<% String ZDATE = ISR.getValue("EFF_DATE"); %>
<% java.text.SimpleDateFormat formatter
= new java.text.SimpleDateFormat("MM/dd/yyyy");
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(formatter.parse(ZDATE));
myDate.add(Calendar.DATE,-1);
      %>
     <td>
    <hbj:textView id="PAYDT" text='<%formatter.format(myDate.getTime())%>' />
    </td>

CJ
0
 

Author Comment

by:dattis
ID: 8036370
CJ, noe I get this error

Original exception: java.text.ParseException: Unparseable date: "02/27/2003"
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036423
even with the last code bit?


The following test works for me:
<% String ZDATE = "02/27/2003";%>
<% java.text.SimpleDateFormat formatter
= new java.text.SimpleDateFormat("MM/dd/yyyy");
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(formatter.parse(ZDATE));
myDate.add(java.util.Calendar.DATE,-1);
     %>
    <td>
   <%=formatter.format(myDate.getTime())%>
   </td>
it prints out:
02/26/2003

so the following code should work for you:

<% String ZDATE = ISR.getValue("EFF_DATE"); %>
<% java.text.SimpleDateFormat formatter
= new java.text.SimpleDateFormat("MM/dd/yyyy");
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(formatter.parse(ZDATE));
myDate.add(java.util.Calendar.DATE,-1);
     %>
    <td>
   <hbj:textView id="PAYDT" text='<%formatter.format(myDate.getTime())%>' />
   </td>

CJ
0
 

Author Comment

by:dattis
ID: 8036590
it is still the same error
java.text.ParseException: Unparseable date: "02/27/2003"

even if I use <% String ZDATE = "02/27/2003";%>
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036744
<% String ZDATE = "02/24/2003";%>
<% java.text.SimpleDateFormat formatter
= new java.text.SimpleDateFormat("MM/dd/yyyy");
java.util.Calendar myDate=java.util.Calendar.getInstance();
myDate.setTime(formatter.parse(ZDATE));
myDate.add(java.util.Calendar.DATE,-1);
     %>
    <td>
   <%=formatter.format(myDate.getTime())%>
   </td>

This works for me with no problem.

can you post your current code.

CJ

0
 
LVL 19

Accepted Solution

by:
cheekycj earned 200 total points
ID: 8036755
make sure you are not pulling cached JSP code.

delete the compiled code from your work directory.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036909
Cached code :-)  Always a problem.

Glad its working and Thanx for the "A"

CJ
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
"Any files you do not have backed up in at least two [other] places are files you do not care about."
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question