Languages
[Edit]
EN

Java - ZonedDateTime minus milliseconds

2 points
Created by:
ParaEagle
484

Quick solution:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Example {

    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy_MM_dd__HH_mm_ss_SSS");

        ZonedDateTime zonedDateTimeNow = ZonedDateTime.now();
        System.out.println(zonedDateTimeNow.format(formatter) + " <- NOW");

        // minus 500 millis
        // 1_000_000_000 == 1 millis
        // 10^9 == 1 millis
        zonedDateTimeNow = zonedDateTimeNow.minusNanos(1000000000L / 2);

        System.out.println(zonedDateTimeNow.format(formatter) + " <- minus 500 millis");
        System.out.println("_____________________^");
    }
}

Output:

2021_09_26__17_49_54_967 <- NOW
2021_09_26__17_49_54_467 <- minus 500 millis
_____________________^

 

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join