?
Solved

Date manipulation in a JSP

Posted on 2003-02-27
23
Medium Priority
?
391 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
  • 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
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.

 

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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

This article explains how to use the rsync command to create backups and sync data across hosts. Rsync is a very useful command that is often used to copy data, make backups, migrate hosts, and bridge the gap between site staging and production envi…
These seven tips can help you create an extraordinary website, one that captivates audiences and has them wanting to return regularly for more. Keep reading to find out what your site is missing and what you need to add!
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

615 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