Supercharger Posted October 20, 2014 Share Posted October 20, 2014 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. Link to comment Share on other sites More sharing options...
Apaec Posted October 20, 2014 Share Posted October 20, 2014 (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 October 20, 2014 by Apaec Link to comment Share on other sites More sharing options...
Fay Posted October 20, 2014 Share Posted October 20, 2014 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); } Link to comment Share on other sites More sharing options...
Dog_ Posted October 20, 2014 Share Posted October 20, 2014 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 2 Link to comment Share on other sites More sharing options...
Apaec Posted October 20, 2014 Share Posted October 20, 2014 pls no ? 1 Link to comment Share on other sites More sharing options...
Dog_ Posted October 20, 2014 Share Posted October 20, 2014 ? DateFormat BAD Link to comment Share on other sites More sharing options...
Fay Posted October 20, 2014 Share Posted October 20, 2014 DateFormat BAD Could have at least trimmed down the quote to show that. Also could have elaborated a lot more... Link to comment Share on other sites More sharing options...
Apaec Posted October 20, 2014 Share Posted October 20, 2014 (edited) Could have at least trimmed down the quote to show that. Also could have elaborated a lot more... Also he coulda helped instead of critisizing other people helping Edited October 20, 2014 by Apaec Link to comment Share on other sites More sharing options...
Supercharger Posted October 20, 2014 Author Share Posted October 20, 2014 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 Link to comment Share on other sites More sharing options...
Dog_ Posted October 20, 2014 Share Posted October 20, 2014 Could have at least trimmed down the quote to show that. Also could have elaborated a lot more... Also he coulda helped instead of critisizing other people helping read the jdocs for SimpleDataFormat#format and you'll see why. and the other method was just ugly Link to comment Share on other sites More sharing options...
Supercharger Posted October 20, 2014 Author Share Posted October 20, 2014 read the jdocs for SimpleDataFormat#format and you'll see why. and the other method was just ugly Got a link to the JavaDoc? Link to comment Share on other sites More sharing options...
Dog_ Posted October 20, 2014 Share Posted October 20, 2014 Got a link to the JavaDoc? http://lmgtfy.com/?q=simpledateformat sorry...i just had to. Link to comment Share on other sites More sharing options...
Czar Posted October 20, 2014 Share Posted October 20, 2014 (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 October 20, 2014 by Czar 1 Link to comment Share on other sites More sharing options...
Fay Posted October 21, 2014 Share Posted October 21, 2014 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! Link to comment Share on other sites More sharing options...
Swizzbeat Posted October 21, 2014 Share Posted October 21, 2014 (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 October 21, 2014 by Swizzbeat Link to comment Share on other sites More sharing options...