Java Programming Tip #1

Posted April 2, 1998

Java Programming Tip #1
 AS/400 - Timestamp, Time, Date - data type format with JDBC.

Java SimpleDateFormat class allows quick and easy formatting of date and time values. This is very handy for converting and using AS/400 timestamp values.

AS/400 timestamps are received and returned as strings. Several AS/400 timestamp formats are possible including;  yyyy-mm-dd-hh.mm.ss.nnnnnn in this form leading zeros can be omitted from the month, day and hour parts, yyyymmddhhmmss is also acceptable but leading zeros in each part must be included. Trailing zeros in the seconds fraction are optional. Hours are 00 to 24 with 00 hour lasting from 12:00 midnight to 1:00 AM.  Hour 24 is valid only in yyyymmdd240000 and is midnight ending yyyymmdd.

See the Java documentation for java.text.SimpleDateFormat to learn the all pattern matching characters used and what output they produce. Here are some examples relevant to AS/400 timestamp, date and time values. The first example creates an object named AS400TSFormat belonging to the Java class SimpleDateFormat. The characters in the constructor argument have special meanings and create a pattern matching format for methods in SimpleDateFormat. The second and third examples create Java objects named AS400DTFormat and AS400TMFormat.
 
    private SimpleDateFormat AS400TSFormat =
                new SimpleDateFormat ("yyyy-MM-dd-HH.mm.ss.SSS");

    private SimpleDateFormat AS400DTFormat =
                new SimpleDateFormat ("yyyy-MM-dd");

    private SimpleDateFormat AS400TMFormat =
                new SimpleDateFormat ("hh:mm:ss");

    public void sampleDateMethod( Date d )
    {
           long now = System.currentTimeMillis();
           Date CurInvDate = new java.util.Date( now );
           String sam1, sam2, sam3, sam4, sam5, sam6, stmtSQL;
 
           sam1 = AS400TSFormat.format( CurInvDate );
           sam2 = AS400DTFormat.format( CurInvDate );
           sam3 = AS400TMFormat.format( CurInvDate );
           sam4 = AS400TSFormat.format( d );
           sam5 = AS400DTFormat.format( d );
           sam6 = AS400TMFormat.format( d );

           stmtSQL = "INSERT INTO ALLDATES VALUES ('" +
                                   sam1 + "','" + sam2 + "','" + sam3 + "','" +
                                   sam4 + "','" + sam5 + "','" + sam6 + "')";

           runJDBCstatement( stmtSQL, UPDATE );
    }
 

This Java method formats two dates (all date and time information is contained in a java.util.Date object) "now" is the current date, "d" is some date passed to this method as a parameter. Each of the two dates is formatted into three different formats then placed in a JDBC SQL statement string and run (in another method named "runJDBCstatement"). The AS/400 table ALLDATES has six fields with data types; timestamp, date, time, timestamp, date, time. Note the samx variables (String objects) are all strings so each "','" group is a double quote, single quote, comma, single quote, double quote, to place the strings in single quotes when they are sent to the AS/400.

In summary all your timestamp, date and time values may be kept and manipulated in a Java program as Date objects then formatted to place in an AS/400 or other database (or for screen display) with SimpleDateFormat objects you create once in the program. Also see the Java documentation for java.text.DateFormat.

South Fork Software would like to put this experience to work for you.
507 875 2428
72640.2025@compuserve.com
fpvw@sfsw.com

Mail to: South Fork Software