• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

how to set time in hh:mm:ss Format?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code..

class TimeFormat
{
public static void main(String ar[])
{
long sec=3650;
long min=0;
int hour=0;
while(sec>=60)
{
sec=sec-60;
min++;
while(min>=60)
{
min=min-60;
hour++;
}
}
System.out.println(hour+":"+min+":"+sec);
}
}

output: 1:0:50..... but i want it in 01:00:50

Friends please help regarding this..,

Thanks in advance
 
Marshal
Posts: 74702
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Is your name really "java?" You know there is a policy about names on the Ranch?

There are several ways of setting time format.
  • 1: Find a class which does it for you. SimpleDateFormat is a possibility; I haven't used it myself.
  • 2: Use the formatting capabilities described in Formatter, (the same method is described in the Java Tutorial).
  • 3: There are probably classes in things like Jakarta Commons which will do the same for you.
  • Using formatted Strings (no 2) is the nearest to what you are doing already.

    CR
     
    Campbell Ritchie
    Marshal
    Posts: 74702
    335
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    BTW: You will get a far more elegant solution to adding times by using the % operator rather than "-60".
     
    Ranch Hand
    Posts: 119
    Hibernate Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    You can try to use System.out.printf() method to format your printing output.
    reply
      Bookmark Topic Watch Topic
    • New Topic