Skip to content

Instantly share code, notes, and snippets.

@aterai
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aterai/c0d285e1a6f7178dc9e7 to your computer and use it in GitHub Desktop.
Save aterai/c0d285e1a6f7178dc9e7 to your computer and use it in GitHub Desktop.
Duration format test
import java.time.*;
import java.time.temporal.*;
public class DurationFormatTest {
public static void main(String... args) throws Exception {
for (int i = 0; i < 10; i++) {
long startTime = System.nanoTime();
Thread.sleep(1000, 888888);
long endTime = System.nanoTime();
System.out.println(endTime - startTime);
test1(endTime - startTime);
Duration d = Duration.ofNanos(endTime - startTime);
System.out.println(d.toString());
test2(d);
}
}
private static void test1(long n) {
long s = (long) (n * 1e-9);
long hours = s / 3600;
int minutes = (int) ((s % 3600) / 60);
int seconds = (int) (s % 60);
int millis = (int) Math.round(n * 1e-6 - seconds * 1e3);
System.out.format("%d:%d:%d %03d%n", hours, minutes, seconds, millis);
}
private static void test2(Duration d) {
long s = d.getSeconds();
int n = d.getNano();
long hours = s / 3600;
int minutes = (int) ((s % 3600) / 60);
int seconds = (int) (s % 60);
int millis = (int) Math.round(n * 1e-6);
System.out.format("%d:%d:%d %03d%n", hours, minutes, seconds, millis);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment