Jump to content

Hh:mm:ss Run Time Method?


Recommended Posts

Posted (edited)

Well you could use a stringbuilder but probably easier to use a date formatter 

                Date date = new Date(System.currentTimeMillis() - startTime);
		DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
		String dateFormatted = formatter.format(date);

Where startTime is a variable defined as:

	long startTime = System.currentTimeMillis();

Hope I helped

Apaec

 

 

Edit: As I was saying before, here's an example of a timeformatter using a stringbuilder:

public static String timeFormat(long time) {
		StringBuilder t = new StringBuilder();
		long total_secs = time / 1000L;
		long total_mins = total_secs / 60L;
		long total_hrs = total_mins / 60L;
		long total_days = total_hrs / 24L;
		int secs = (int) total_secs % 60;
		int mins = (int) total_mins % 60;
		int hrs = (int) total_hrs % 24;
		int days = (int) total_days;
		if (days < 10) {
			t.append("0");
		}
		t.append(days).append(":");
		if (hrs < 10) {
			t.append("0");
		}
		t.append(hrs).append(":");
		if (mins < 10) {
			t.append("0");
		}
		t.append(mins).append(":");
		if (secs < 10) {
			t.append("0");
		}
		t.append(secs);
		return t.toString();
	}

Hey Guys,

 

Anyone help me out back to scripting after a few years and have forgotten a few things.

 

How do I get my run time in HH:MM:SS, atm it's like 23742384.

 

Cheers.

 

Edited by Apaec
Posted

I think I took this from a scripter a while back so no credit of my own:

 

function for formatting time string.

	public String format(long time) {
		StringBuilder string = new StringBuilder();
		long totalSeconds = time / 1000L;
		long totalMinutes = totalSeconds / 60L;
		long totalHours = totalMinutes / 60L;
		int seconds = (int) totalSeconds % 60;
		int minutes = (int) totalMinutes % 60;
		int hours = (int) totalHours % 24;
		if (hours > 0) {
			string.append((new StringBuilder(String.valueOf(hours))).append(
					":").toString());
		}
		if (minutes > 0) {
			string.append((new StringBuilder(String.valueOf(minutes))).append(
					":").toString());
		}
		string.append((new StringBuilder(String.valueOf(seconds))).append(":")
				.toString());
		return string.toString();
	}

onStart function:

	@Override
	public void onStart() {
		startTime = System.currentTimeMillis();
	}

onPaint function:

	public void onPaint(Graphics2D g) {
		long elapsed = System.currentTimeMillis() - startTime;
		g.drawString((new StringBuilder("Time: ")).append(format(elapsed)).toString(), 50, 50);
	}
Posted

 

Well you could use a stringbuilder but probably easier to use a date formatter 

                Date date = new Date(System.currentTimeMillis() - startTime);
		DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
		String dateFormatted = formatter.format(date);

Where startTime is a variable defined as:

	long startTime = System.currentTimeMillis();

Hope I helped

Apaec

 

 

Edit: As I was saying before, here's an example of a timeformatter using a stringbuilder:

public static String timeFormat(long time) {
		StringBuilder t = new StringBuilder();
		long total_secs = time / 1000L;
		long total_mins = total_secs / 60L;
		long total_hrs = total_mins / 60L;
		long total_days = total_hrs / 24L;
		int secs = (int) total_secs % 60;
		int mins = (int) total_mins % 60;
		int hrs = (int) total_hrs % 24;
		int days = (int) total_days;
		if (days < 10) {
			t.append("0");
		}
		t.append(days).append(":");
		if (hrs < 10) {
			t.append("0");
		}
		t.append(hrs).append(":");
		if (mins < 10) {
			t.append("0");
		}
		t.append(mins).append(":");
		if (secs < 10) {
			t.append("0");
		}
		t.append(secs);
		return t.toString();
	}

pls no

  • Like 2
Posted
public static String timeFormat(long time) {
		StringBuilder t = new StringBuilder();
		long total_secs = time / 1000L;
		long total_mins = total_secs / 60L;
		long total_hrs = total_mins / 60L;
		long total_days = total_hrs / 24L;
		int secs = (int) total_secs % 60;
		int mins = (int) total_mins % 60;
		int hrs = (int) total_hrs % 24;
		int days = (int) total_days;
		if (days < 10) {
			t.append("0");
		}
		t.append(days).append(":");
		if (hrs < 10) {
			t.append("0");
		}
		t.append(hrs).append(":");
		if (mins < 10) {
			t.append("0");
		}
		t.append(mins).append(":");
		if (secs < 10) {
			t.append("0");
		}
		t.append(secs);
		return t.toString();
	}

I'm using this method, but it's only showing H:MM:SS. How can I change it to HH:MM:SS?

 

I've tried numerous things and can't figure it out :X

Posted (edited)

Don't make things so complicated.

public String formatTime(final long time) {
final int sec = (int) (time / 1000), h = sec / 3600, m = sec / 60 % 60, s = sec % 60;
return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
}
Edited by Czar
  • Like 1
Posted

Don't make things so complicated.

public String formatTime(final long time) {final int sec = (int) (time / 1000), h = sec / 3600, m = sec / 60 % 60, s = sec % 60;return (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);}
And this is why I need scripting friends that are better than me. Beautiful code!
Posted (edited)
public static String timeFormat(long time) {
		StringBuilder t = new StringBuilder();
		long total_secs = time / 1000L;
		long total_mins = total_secs / 60L;
		long total_hrs = total_mins / 60L;
		long total_days = total_hrs / 24L;
		int secs = (int) total_secs % 60;
		int mins = (int) total_mins % 60;
		int hrs = (int) total_hrs % 24;
		int days = (int) total_days;
		if (days < 10) {
			t.append("0");
		}
		t.append(days).append(":");
		if (hrs < 10) {
			t.append("0");
		}
		t.append(hrs).append(":");
		if (mins < 10) {
			t.append("0");
		}
		t.append(mins).append(":");
		if (secs < 10) {
			t.append("0");
		}
		t.append(secs);
		return t.toString();
	}

 

Don't make things so complicated.

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

This is the same thing as above but even worse.

 

I really don't see why people insist on making their code unreadable just because "it saves lines". I know the ternary is actually slower in C# when compared to nested if statements because of the JIT trying to make optimizations, not sure about Java.

Edited by Swizzbeat
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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