This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to set time in hh:mm:ss Format? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to set time in hh:mm:ss Format?" Watch "how to set time in hh:mm:ss Format?" New topic
Author

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

chinna
Greenhorn

Joined: May 01, 2007
Posts: 4
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
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
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37958
        
      22
    BTW: You will get a far more elegant solution to adding times by using the % operator rather than "-60".
    Wayan Saryada
    Ranch Hand

    Joined: Feb 05, 2004
    Posts: 104

    Hi,

    You can try to use System.out.printf() method to format your printing output.


    Website: Learn Java by Examples
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to set time in hh:mm:ss Format?
     
    Similar Threads
    Clock
    Date Comparison
    how to update java.util.Calendar in jsp?
    Finding Elapsed Time
    Back ground sound is not working in netbeans.Is there any alternative?