Synchronizing access to SimpleDateFormat

前端 未结 9 1480
名媛妹妹
名媛妹妹 2020-12-02 06:56

The javadoc for SimpleDateFormat states that SimpleDateFormat is not synchronized.

\"Date formats are not synchronized. It is recommended to create

9条回答
  •  一向
    一向 (楼主)
    2020-12-02 07:23

    If you are using Java 8, you may want to use java.time.format.DateTimeFormatter:

    This class is immutable and thread-safe.

    e.g.:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    String str = new java.util.Date().toInstant()
                                     .atZone(ZoneId.systemDefault())
                                     .format(formatter);
    

提交回复
热议问题