issue accessing Date object from within a scriptlet embedded in JSP

D

Damon Getsman

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:

<fmt:parseDate var="inDate" value="${punch.starttime}"
pattern="yyyy-MM-dd HH:mm:ss" />
<fmt:parseDate 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 >= 8) && (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
 
D

Damon Getsman

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.
 
C

cteb

Damon said:
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
 
N

Nigel Wade

Damon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top