1 Reply Latest reply: May 8, 2012 1:51 PM by morgalr RSS

    result of java.sql.timestamp

    pgoovaerts
      I have a project which generates file on our fileserver. To be sure to get unique filenames, i make use of the following code:

      java.util.Date today = new java.util.Date();
      String sTimeStamp = new java.sql.Timestamp(today.getTime()).toString();

      Now it appears that the results are not always same in length:
      2012050414220623
      2012050710032132
      20120504140937333
      20120504150311857
      20120504161851309
      20120504163002125
      20120507100451728

      As you can see, the first two have one digit short, why is this?


      thx.
        • 1. Re: result of java.sql.timestamp
          morgalr
          Since the last part of the SQL TimeStamp has a decimal value representing out to thousandths of a second, the trailing 0 is supressed.

          You can use someething like this:
          public class jTimeJunk {
            public static void main(String[] args) {
              for(int i=0; i<100; i++){
                long l = new java.util.Date().getTime();
                String s = ((new java.sql.Timestamp(l).toString()+"000").substring(0, 23)).replaceAll("-|[ ]|:|[.]", "");
                System.out.println(s);
              }
            }
          }
          To ensure you have the number of digits you desire or you can use a simple number format on the decimal portion.