Languages
[Edit]
EN

Java date time now with milliseconds - yyyy-MM-dd HH:mm:ss.milliseconds or yyyy_MM_dd__HH_mm_ss_milliseconds (java time SSS format)

5 points
Created by:
Marcin
18790

Quick solution:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class JavaDateTimeNowExamples {

    public static String createDateTimeNowMs() {
        // 2021_05_15__12_29_12_627
        return DateTimeFormatter.ofPattern("yyyy_MM_dd__HH_mm_ss_SSS")
                .format(LocalDateTime.now());
    }

    public static String createDateTimeNow() {
        // 2021_05_15__12_29_12
        return DateTimeFormatter.ofPattern("yyyy_MM_dd__HH_mm_ss")
                .format(LocalDateTime.now());
    }

    public static void main(String[] args) {

        System.out.println(createDateTimeNowMs()); // 2021_05_15__12_29_12_627
        System.out.println(createDateTimeNow());   // 2021_05_15__12_29_12
    }
}

Output:

2021_05_15__12_29_12_627
2021_05_15__12_29_12

 

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