Java Quick Tip of the Day: Getting the Date in YYYYMMDD Format

There are times when you need to return a date as string in YYYYMMDD format. Here’s a simple way to do it:

    import java.text.*;
    ...
    ...
    ...
    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
    java.util.Date date = java.util.Calendar.getInstance().getTime();
    System.out.println(sf.format(date));

Java Quick Tip of the Day: Selecting Current TimeStamp for DB2

There are times when you may want multiple programs to retrieve a current timestamp from a central location. This can be accomplished by selecting the timestamp from a database server. To return the current timestamp from DB2 execute the following query:


     ResultSet rs;
     rs = stmt.executeQuery("select distinct(current timestamp)
          from sysibm.sysdummy1");
     rs.next();
     java.util.Date today = rs.getTimestamp(1);

Notice that I used a table called “sysibm.sysdummy1”. Sysdummy1 is a special purpose table for uses such as this one. Additionally, you can use any db2 table that you have access to.