CKTIS / Java
TimeUnit — time units and conversions
TimeUnit describes time units used in operations such as timeouts. It converts long values, truncating fractional parts or saturating on overflow.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
TimeUnit.MILLISECONDS.toNanos(long duration)
TimeUnit.NANOSECONDS.convert(long sourceDuration, TimeUnit sourceUnit)java.util.concurrent.TimeUnit is an enum describing time units, used in operations such as timeouts. It does not store or measure time itself.
TimeUnit.MILLISECONDS denotes milliseconds. The instance method TimeUnit.toNanos(long duration) interprets its argument in the receiver's unit: TimeUnit.MILLISECONDS.toNanos(2L) returns 2000000L. The equivalent call is TimeUnit.NANOSECONDS.convert(2L, TimeUnit.MILLISECONDS) — the receiver of convert specifies the target unit.
Conversion to a coarser unit discards the fractional part: 999 milliseconds becomes 0 seconds. On overflow, the result is clamped to Long.MIN_VALUE or Long.MAX_VALUE instead of wrapping around.
A nanosecond unit does not guarantee nanosecond timing accuracy. The example performs only deterministic conversions, without waiting.
Examples
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
System.out.println(TimeUnit.MILLISECONDS.toNanos(2L));
System.out.println(TimeUnit.MILLISECONDS.toSeconds(999L));
System.out.println(TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE));
}
}Result
2000000 0 9223372036854775807
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.