如何在Java中以YYYY-MM-DD HH:MI:秒毫秒格式获取当前时间?

百般思念 提交于 2020-08-17 03:34:18

问题:

The code below gives me the current time. 下面的代码为我提供了当前时间。 But it does not tell anything about milliseconds. 但这并不能说明毫秒。

public static String getCurrentTimeStamp() {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
    Date now = new Date();
    String strDate = sdfDate.format(now);
    return strDate;
}

I get date in the format 2009-09-22 16:47:08 (YYYY-MM-DD HH:MI:Sec) . 我以2009-09-22 16:47:08 (YYYY-MM-DD HH:MI:Sec)的格式获取日期。

But I want to retrieve the current time in the format 2009-09-22 16:47:08.128 ((YYYY-MM-DD HH:MI:Sec.Ms)- where 128 tells the millisecond. 但是我想以2009-09-22 16:47:08.128 ((YYYY-MM-DD HH:MI:Sec.Ms))的格式检索当前时间-其中128表示毫秒。

SimpleTextFormat will work fine. SimpleTextFormat将正常工作。 Here the lowest unit of time is second, but how do I get millisecond as well? 在这里,最小的时间单位是秒,但是如何获得毫秒呢?


解决方案:

参考一: https://stackoom.com/question/67iq/如何在Java中以YYYY-MM-DD-HH-MI-秒毫秒格式获取当前时间
参考二: https://oldbug.net/q/67iq/How-to-get-the-current-time-in-YYYY-MM-DD-HH-MI-Sec-Millisecond-format-in-Java
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!