Jump to content

Formatting time to 00:00:00:00


Recommended Posts

Posted
	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
Posted

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
Posted

 

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
Posted (edited)

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
Posted

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)
Posted

 

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 :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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