Programming Tips - Java: convert nanoTime() to currentTimeMillis()

Date: 2024nov7 Language: Java Q. Java: convert nanoTime() to currentTimeMillis() A. You cannot convert System.nanoTime() to System.currentTimeMilli() because they are reporting the time since different points. So you cannot simply divide the nanotime by 10^6 to get the milliseconds.
import java.lang.System; class Main { public static void main(String args[]) { final long ms = System.currentTimeMillis(); final long ns = System.nanoTime(); System.out.println("current milliseconds=" + ms); System.out.println(" current nanoeconds=" + ns); final long msConv = ns / 1000000; if (msConv == ms) { System.out.println("Converted ms is the same as me"); } else { System.out.println("Converted ms isn't the same as ms"); } } }
Produces:
current milliseconds=1730994738510 current nanoeconds=752938024724354 Converted ms isn't the same as ms