#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    43
    Rep Power
    11

    Please help ! How to format time as military time


    I am trying to display military time(insteed of A.M/P.M, hours from 1 to 24) on my JSP.
    Is there any way to format time as military time.

    Thanks in advance
  2. #2
  3. Prom night: 1973
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2001
    Posts
    1,156
    Rep Power
    24
    Code:
    Date now = new Date();
    SimpleDateFormat militaryTime = new SimpleDateFormat("kk:mm");
    System.out.println("The time is " + militaryTime.format(now));
    The formatting codes for dates are at SimpleDateFormat
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    43
    Rep Power
    11
    Thanks for your response.
    My problem is I want to display (date + time) like
    03/17/2004 : 14:53:00

    This is what I am trying but not working.

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy:hh:mm:ss ") ;

    formattedAppDate = sdf.format(appointmentDate) + " " +
    DateFormat.getTimeInstance(DateFormat.SHORT).format(appointmentDate) ;
    System.out.println("apptdatetime is " + formattedAppDate);

    I will really appriciate if you help me out.

    Thanks again.
  6. #4
  7. Prom night: 1973
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2001
    Posts
    1,156
    Rep Power
    24
    Just put the extra characters inside the SimpleDateFormat pattern.
    Code:
    Date now = new Date();
    SimpleDateFormat militaryTime = new SimpleDateFormat("MM/dd/yyyy : kk:mm:ss");
    System.out.println("The time is " + militaryTime.format(now));
    If you'd rather get the date and time separately
    Code:
    SimpleDateFormat militaryYMD = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat militaryTime = new SimpleDateFormat("kk:mm:ss");
    System.out.println("The time is " + militaryYMD.format(now) + " : " + militaryTime.format(now));
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    43
    Rep Power
    11
    Thanks a lot.This works for me.
    I really appriciate your help.

IMN logo majestic logo threadwatch logo seochat tools logo