Showing posts with label JSTL. Show all posts
Showing posts with label JSTL. Show all posts

Friday, July 28, 2006

Get the Current Date/Time in JSTL

Get the current date like so:
<jsp:useBean id="now" class="java.util.Date" />

Or you can parse a string date into a date object
<fmt:parseDate var="mydate" pattern="yyyy-MM-dd">${stringHoldingDate}</fmt:parseDate>

Now format it as you like (calling formatDate will return the formatted date as a string, place the code inside a <c:set></c:set> to set as a variable; otherwise, it will print out to the screen):
<fmt:formatDate value="${now}" type="both" pattern="MM/dd/yyyy HH:mm:ss" />


You must use type=both otherwise the time will not be parsed, even if the format contains it!

Friday, February 03, 2006

JSTL pointers?

Here's one that can be useful. I created a HashMap of HashMaps (formFields). Each HashMap in the HashMap contains some data about a formfield. I noticed that depending on how I access the inner HashMaps, it either acts like call by value or call by reference. This is under Sun Appserver 8.1.

<c:forEach var="field" items="${formFields}">
<%-- this creates a copy --%>
<c:set var="currentField" value="${field.value}"/>
<%-- this creates a pointer: --%>
<c:set var="currentField" value="${formFields[field.key]}"/>
</c:forEach>

Monday, January 30, 2006

Adding a Few Hours to a Java Date (in JSTL)

I had to add 2 or 12 hours to a date stored as a string. Use the fmt tag library. It contains parseDate, which takes a String and returns a Java Date object. formatDate prints out a Date object as a string. I added the time by manipulating the time zone of each date as follows:

<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

...

<fmt:parsedate value="${BEGIN_DATE}" var="BEGINPLUS12" pattern="MM/dd/yyyy HH:mm"
timezone="GMT+00">

<fmt:formatdate value="${BEGINPLUS12}" type="both" pattern="MM/dd/yyyy HH:mm"
timezone="GMT+12">
<%-- add 12 hours by changing time zone --%>


The type="both" in the formatDate keeps it from discarding the time information.

Used the following two pages to figure this out:
formatDate, parseDate: http://www-128.ibm.com/developerworks/java/library/j-jstl0415/
Java Time Zones: http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimeZone.html

Labels

Blog Archive

Contributors