On Java Development

All things related to Java development, from the perspective of a caveman.

Displaying Date or Time

without comments

Introduction

Dates and Times on the iSeries are often stored as integers where the date will be in “yyyymmdd” format and the time will be in “hhmmss” format. This post shows how to properly format dates so that it appears on the web-page as “yyyy-dd-mm” and times as “hh:mm:ss”. For example, a date stored as “20120102” in the database needs to appear as “2012-01-02” when displayed on a web-page. A time stored as “210815” needs to appear as “21:08:15”. The post also presents information on managed beans, how they are referenced by a RichFaces DataTable and the RichFaces DataTable iterative process.

Managed Beans, Data Tables and Iterative Operations
JSF based tables support iterative operations upon collections of POJOs so that the properties of the POJO can be displayed in the data table as individual rows and columns. For example, consider the following code segment that defines a RichFaces dataTable.

The declaration of a RichFaces DataTable is shown by lines 1-3. It must be made clear that making such a declaration really represents the beginning of a loop construct. The end of the declaration shown on line 15 represents the end of the loop. Each iteration through the supplied collection of rows create the data table’s rows and columns.

Line 1 shows the value keyword used to indicate the list of data in listOfPSPTAUD05 in the managed bean called page01ManagedBean is the source of the records to be displayed in the table.

JavaBean objects managed by a JSF implementation are called managed beans. A managed bean is itself a POJO that has been declared as a managed bean using JSF’s @ManagedBean annotation, shown below.

This annotation is placed at the beginning of the java class to be declared as a managed bean.

Data collected into a managed bean is then made available to the web-page. It might be helpful to think of the managed bean as a RPG data structure holding data to be displayed by the web-page. However, unlike data structures in RPG, managed beans can also contain logic available through method calls.

Continuing, line 2 uses the var keyword to indicate that each POJO contained within listOfPSPTAUD05 is declared as a variable named auditRecord. This variable represents the individual record and the column data within it is then referenced by each column definition.

Together, the value and var keywords are simply saying the table rows are in the collection contained within the managed bean (“#{page01ManagedBean.listOfPSPTAUD05}”) and the each individual row is to be extracted and placed into the variable called auditRecord.

 
Extracting and Formatting the Date column
Lines 6-13 above declares the column for the date field to be displayed, with lines 7-9 defining the column’s header and lines 10-12 defining the column’s data element.

The magic of displaying the date occurs on line 1, below. It is calling a method in the managed bean page01ManagedBean called convertYYYYMMDDToDate and passes the value representing the integer date in field SCDT01 that is contained in the record auditRecord as the argument.

For a little clarity, the method convertYYYYMMDDToDate being called is shown below. It uses the DateTimeHelper class which is included in baseproject to convert the integer value into a Date object and returns it.

When the Date object is returned to the caller, the column definition’s use of <f:convertDateTime pattern=”yyyy-MM-dd”/> converts the Date object and formats it using the supplied pattern.

 
Using a Converter to Format Time
Time is a bit different than the date problem presented above, due mainly to the fact that unlike the date object to which the integer value was converted, there is no time object as time is embedded into the date object. Because of this, the integer that is the time is simply formatted as a string for display purposes.

To do this, a JSF Converter is created, shown below. The method that does the work is getAsString. The class is annotated as a FacesConverter shown on line 10. The name assigned is the name of the converter and will be referenced in the web-page’s source. The method converts the value by first testing it to ensure it is truly of type Integer, the result of which is placed into the variable hhmmss. Having done this, the next step is to use the DateTimeHelper class to format the integer into the human readable version that is returned to the caller.

The caller is the entry in the web-page, shown below. The output text is obtained by calling the getClaimsProcessTime() method. The <f:converter is employed to indicate the timeConverter presented above should be used to convert the value returned to a string that is then formatted as a properly formatted time.

 
 
Summary
This post showed how to format a date stored as an integer value so that it can be displayed on a web-page while also presenting information on managed beans, how they are referenced by a RichFaces DataTable as well as the RichFaces DataTable iterative process.

Written by admin

March 10th, 2014 at 10:43 am

Leave a Reply

You must be logged in to post a comment.