Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > issue accessing Date object from within a scriptlet embedded in JSP

Reply
Thread Tools

issue accessing Date object from within a scriptlet embedded in JSP

 
 
Damon Getsman
Guest
Posts: n/a
 
      09-23-2008
True to bad style, I'm writing a piece of JSP right now where there's
a bit of actual code scriptlet embedded with <% ... %> tags now.

In the jsp above that scriptlet I'm working with 2 objects of
java.util.Date, called inDate and outDate. When I'm trying to access
the different objects er values or whatever within them, I'm using
standard inDate.minutes and etc. to attempt to do tests on them. Is
there something special that I need to know about accessing this kind
of object from within an embedded scriptlet? I'm getting a server log
error of
An error occurred at line: 169 in the jsp file: /reports.jsp
inDate.hours cannot be resolved or is not a field

When I access that section now; debugging code above the scriptlet has
proven that inDate & outDate are getting their proper values in the
JSP code, though.

Here's a little bit of the code; I'm pretty sure this issue is just
something that I don't understand about either the scope of these
objects (declared at the top of the same JSP page) or how much access
an embedded scriptlet has to variables declared in JSP:

<fmtarseDate var="inDate" value="${punch.starttime}"
pattern="yyyy-MM-dd HH:mm:ss" />
<fmtarseDate var="outDate" value="${punch.endtime}" pattern="yyyy-
MM-dd HH:mm:ss" />

<%
int totalMinutes = 0, units = 0;
if (inDate.hours != outDate.hours)
totalMinutes = (outDate.hours - inDate.hours) * 60;
totalMinutes += (outDate.minutes - inDate.minutes);

if ((totalMinutes >= && (totalMinutes <= 22))
units = 1;
else if (totalMinutes >= 23) {
units = 1;
totalMinutes -= 22;
do {
units += 1;
totalMinutes -= 15;
} while (totalMinutes > 0);
}
%>


I would be very grateful if anybody could help point me in the right
direction on this issue.

Damon Getsman
 
Reply With Quote
 
 
 
 
Damon Getsman
Guest
Posts: n/a
 
      09-23-2008
Duhh, don't mind me. I was trying to access the members of that
object the same way that I did in JSP.

Depreciated getHours, getMinutes, etc, worked just fine. Can anybody
tell me the non-depreciated way to do this? The Calendar object stuff
on the java.sun.com website confused me a bit.
 
Reply With Quote
 
 
 
 
cteb
Guest
Posts: n/a
 
      09-23-2008
Damon Getsman wrote:

> Duhh, don't mind me. I was trying to access the members of that
> object the same way that I did in JSP.
>
> Depreciated getHours, getMinutes, etc, worked just fine. Can anybody
> tell me the non-depreciated way to do this? The Calendar object stuff
> on the java.sun.com website confused me a bit.


Calendar confuses everyone. You have to create a new Calendar object,
GregorianCalendar is a non-abstract subtype, call its setTime method
with your Date object and access it with its get method for each part of
the date you need, e.g. like this:

Calendar cal = new GregorianCalendar();
cal.setTime(inDate);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

hth
... C
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      09-24-2008
Damon Getsman wrote:

> Duhh, don't mind me. I was trying to access the members of that
> object the same way that I did in JSP.
>
> Depreciated getHours, getMinutes, etc, worked just fine. Can anybody
> tell me the non-depreciated way to do this? The Calendar object stuff
> on the java.sun.com website confused me a bit.


You can't get any meaningful hours/mins/secs from a Date object, hence the
reason that those methods are deprecated. A Date object represents an instant
in time, everywhere on the Earth in all timezones simultaneously (and in near
space as well, if you want to get pedantic) - hence "hours" doesn't really mean
anything.

To get hours meaningfully you need a timezone, and DST and all the other baggage
that a Calendar object carries around with it. Unless you want to do everything
in UTC of course.

What you need to do is create yourself an object which is a concrete
implementation of the Calendar interface. This usually means a
GregorianCalendar. By default this will use your local timezone. Then use the
setTime() method of the Calendar object passing it your Date object. You then
use the Calendar.get() method to get the various values, e.g.
Calendar.get(Calendar.HOUR) [or Calendar.HOUR_OF_DAY] for the hours.

--
Nigel Wade
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
jsp:include is not allowing me write scriptlet between <jsp:param> tags ramakrishna Java 0 05-04-2006 12:49 PM
Questions: No PHP, but SSI and Scriptlet Hats HTML 3 06-20-2005 12:01 PM
Share vars between scriptlet and jstl? Robert Mark Bram Java 1 03-20-2005 08:38 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Tag's handler class bsf.scriptlet not found in classpath - help Phil Powell Java 0 11-21-2003 02:33 PM



Advertisments