How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?

后端 未结 15 2219
不思量自难忘°
不思量自难忘° 2020-11-22 15:05

The code below gives me the current time. But it does not tell anything about milliseconds.

public static String getCurrentTimeStamp() {
    SimpleDateForm         


        
15条回答
  •  抹茶落季
    2020-11-22 15:22

    java.text (prior to java 8)

    public static ThreadLocal dateFormat = new ThreadLocal() {
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        };
    };
    
    ...
    dateFormat.get().format(new Date());
    

    java.time

    public static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    
    ...
    dateTimeFormatter.format(LocalDateTime.now());
    

提交回复
热议问题