Last active
August 29, 2015 14:23
-
-
Save aterai/c0d285e1a6f7178dc9e7 to your computer and use it in GitHub Desktop.
Duration format test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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