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
Quick solution:
xxxxxxxxxx
1
import java.time.LocalDateTime;
2
import java.time.format.DateTimeFormatter;
3
4
public class JavaDateTimeNowExamples {
5
6
public static String createDateTimeNowMs() {
7
// 2021_05_15__12_29_12_627
8
return DateTimeFormatter.ofPattern("yyyy_MM_dd__HH_mm_ss_SSS")
9
.format(LocalDateTime.now());
10
}
11
12
public static String createDateTimeNow() {
13
// 2021_05_15__12_29_12
14
return DateTimeFormatter.ofPattern("yyyy_MM_dd__HH_mm_ss")
15
.format(LocalDateTime.now());
16
}
17
18
public static void main(String[] args) {
19
20
System.out.println(createDateTimeNowMs()); // 2021_05_15__12_29_12_627
21
System.out.println(createDateTimeNow()); // 2021_05_15__12_29_12
22
}
23
}
Output:
xxxxxxxxxx
1
2021_05_15__12_29_12_627
2
2021_05_15__12_29_12