Results 1 to 5 of 5
  1. #1
    firewalll is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default Pack Time with "0"

    Dear All,


    private String getTxnDateTime() throws Exception {
    Calendar cal = Calendar.getInstance();

    return Integer.toString(cal.get(Calendar.YEAR)) +
    Integer.toString(cal.get(Calendar.MONTH)+1) +
    Integer.toString(cal.get(Calendar.DAY_OF_MONTH)) +
    Integer.toString(cal.get(Calendar.HOUR_OF_DAY)) +
    Integer.toString(cal.get(Calendar.MINUTE)) +
    Integer.toString(cal.get(Calendar.SECOND)) ;
    }


    it will return sth like "2009911195551"
    But I would like to return fix length 14
    that is "20090911195551"
    what should I do to pack the value with "0"???

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    System.out.printf.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Even better is SimpleDateFormat

  4. #4
    firewalll is offline Member
    Join Date
    Sep 2009
    Posts
    7
    Rep Power
    0

    Default

    Problem Solved with the following code

    private String getTxnDateTime() throws Exception {
    Calendar cal = Calendar.getInstance();
    String pattern="00";
    java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
    String tempTime = null;
    tempTime = Integer.toString(cal.get(Calendar.YEAR)) +
    df.format(cal.get(Calendar.MONTH)+1) +
    df.format(cal.get(Calendar.DAY_OF_MONTH)) +
    df.format(cal.get(Calendar.HOUR_OF_DAY)) +
    df.format(cal.get(Calendar.MINUTE)) +
    df.format(cal.get(Calendar.SECOND)) ;


    return tempTime ;
    }

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  2. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  3. Replies: 0
    Last Post: 11-22-2008, 02:49 AM
  4. Using Dr. Java for first time...."No compiler"
    By gallimaufry in forum New To Java
    Replies: 3
    Last Post: 10-28-2008, 02:02 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •