Jump to content

Formatting time to 00:00:00:00


Apaec

Recommended Posts

	public static String formatTimeDHMS(final long time) {
		final int sec = (int) (time / 1000), d = sec / 86400, h = sec / 3600 % 24, m = sec / 60 % 60, s = sec % 60;
		return (d < 10 ? "0" + d : d) + ":" + (h < 10 ? "0" + h : h) + ":"
				+ (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
	}

where time is in milliseconds.

 

Implementation:

g.drawString("Runtime: " + formatTimeDHMS(timer.getElapsed()), 100,100);

where g = graphics2d

and timer is your timer from the start of the script

and getelapsed is a method from your timer class returning the time elapsed in millis

 

This ill be formatted as: 00:00:00:00.

                                        d: h: m: s

 

Apa

  • Like 3
Link to comment
Share on other sites

How about 

 

Class:

 public class Timer {

        private Instant start;

        private static final String formatter = "%02d";

        public Timer() {
            reset();
        }

        public void reset() {
            start = Instant.now();
        }

        public Duration duration() {
            return Duration.between(start, Instant.now());
        }

        @Override
        public String toString() {
            Duration duration = duration();
            return String.format(formatter, duration.toHours()) + " : " + String.format(formatter, duration.toMinutes() % 60) + " : " + String.format(formatter, duration.getSeconds() % 60);
        }

    }

Setup:

 Timer t = new Timer();

Usage:

  g.drawString("Time: " + t.toString(), 55, 401);
  • Like 1
Link to comment
Share on other sites

 

How about 

 

Class:

 public class Timer {

        private Instant start;

        private static final String formatter = "%02d";

        public Timer() {
            reset();
        }

        public void reset() {
            start = Instant.now();
        }

        public Duration duration() {
            return Duration.between(start, Instant.now());
        }

        @Override
        public String toString() {
            Duration duration = duration();
            return String.format(formatter, duration.toHours()) + " : " + String.format(formatter, duration.toMinutes() % 60) + " : " + String.format(formatter, duration.getSeconds() % 60);
        }

    }

Setup:

 Timer t = new Timer();

Usage:

  g.drawString("Time: " + t.toString(), 55, 401);

 

That works too

i have my method also built into my timer class

 

I dislike java's duration and instant classes tho. 

 

your method does not format days though, but that would be easy to add.

 

apa

  • Like 1
Link to comment
Share on other sites

That works too

i have my method also built into my timer class

 

I dislike java's duration and instant classes tho. 

 

your method does not format days though, but that would be easy to add.

 

apa

 

Ah yes yes, think I will add that in now. biggrin.png

 

Guess i will add:

  return String.format(formatter, duration.toDays()) + " : " +  String.format(formatter, duration.toHours()) + " : " + String.format(formatter, duration.toMinutes() % 60) + " : " + String.format(formatter, duration.getSeconds() % 60);
Edited by Dark Magician
Link to comment
Share on other sites

in timer class

 public static String format(long milliSeconds)
   {
       long secs = milliSeconds / 1000L;
       return String.format("%02d:%02d:%02d:%02d", new Object[] {
       Long.valueOf(milliSeconds / (1000*60*60*24)) , Long.valueOf((secs / 3600L) % 24), Long.valueOf((secs % 3600L) / 60L), Long.valueOf(secs % 60L)
       });
   }

in main 

final long lngStartTime = System.currentTimeMillis(); 

on paint

 g.drawString(""+Timer.format(System.currentTimeMillis() - lngStartTime), X, Y)
Link to comment
Share on other sites

 

in timer class

 public static String format(long milliSeconds)
   {
       long secs = milliSeconds / 1000L;
       return String.format("%02d:%02d:%02d:%02d", new Object[] {
       Long.valueOf(milliSeconds / (1000*60*60*24)) , Long.valueOf((secs / 3600L) % 24), Long.valueOf((secs % 3600L) / 60L), Long.valueOf(secs % 60L)
       });
   }

in main 

final long lngStartTime = System.currentTimeMillis(); 

on paint

 g.drawString(""+Timer.format(System.currentTimeMillis() - lngStartTime), X, Y)

 

Defining your start time at the top of your code as you have suggested is a risky thing to do - it can sometimes return unexpected results

apa :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...