issue accessing Date object from within a scriptlet embedded in JSP

Discussion in 'Java' started by Damon Getsman, Sep 23, 2008.

  1. 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
     
    Damon Getsman, Sep 23, 2008
    #1
    1. Advertising

  2. Re: issue accessing Date object from within a scriptlet embedded inJSP

    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.
     
    Damon Getsman, Sep 23, 2008
    #2
    1. Advertising

  3. Damon Getsman

    cteb Guest

    Re: issue accessing Date object from within a scriptlet embeddedin JSP

    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
     
    cteb, Sep 23, 2008
    #3
  4. Damon Getsman

    Nigel Wade Guest

    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
     
    Nigel Wade, Sep 24, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Powell
    Replies:
    0
    Views:
    1,664
    Phil Powell
    Nov 21, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,332
    Michael Borgwardt
    May 30, 2004
  3. Robert Mark Bram

    Share vars between scriptlet and jstl?

    Robert Mark Bram, Mar 20, 2005, in forum: Java
    Replies:
    1
    Views:
    15,392
    Ryan Stewart
    Mar 20, 2005
  4. Hats
    Replies:
    3
    Views:
    474
    Spartanicus
    Jun 20, 2005
  5. ramakrishna
    Replies:
    0
    Views:
    1,278
    ramakrishna
    May 4, 2006
Loading...

Share This Page