下面的代码给出了当前时间。但是它并没有告诉我们毫秒。

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;
}

我有一个日期,格式是YYYY-MM-DD HH:MM:SS(2009-09-22 16:47:08)。

但是我想以YYYY-MM-DD HH:MM:SS的格式检索当前时间。MS(2009-09-22 16:47:08.128,其中128为毫秒)。

SimpleTextFormat可以很好地工作。这里最低的时间单位是秒,但我怎么也能得到毫秒呢?


当前回答

最简单的方法是(在Java 8之前)使用,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

但是SimpleDateFormat不是线程安全的。既不java.util.Date。这将导致潜在的并发问题。现有的设计存在很多问题。为了克服这些问题,在Java 8中我们有一个单独的包,叫做Java .time。这个Java SE 8日期和时间文档对它有一个很好的概述。

所以在Java 8中,如下所示就可以实现(格式化当前日期/时间),

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

需要注意的一点是,它是在流行的第三方库joda-time的帮助下开发的,

该项目由Joda-Time的作者(Stephen Colebourne)和Oracle共同领导,在JSR 310下,并将出现在新的Java SE 8包Java .time中。

但是现在joda-time已被弃用,并要求用户迁移到新的java.time。

注意,从Java SE 8开始,用户被要求迁移到Java。time (JSR-310)——JDK的核心部分,取代了这个项目

说到这里,

如果你有一个日历实例,你可以使用下面的方法将它转换为新的java.time,

    Calendar calendar = Calendar.getInstance();
    long longValue = calendar.getTimeInMillis();         

    LocalDateTime date =
            LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue), ZoneId.systemDefault());
    String formattedString = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));

    System.out.println(date.toString()); // 2018-03-06T15:56:53.634
    System.out.println(formattedString); // 2018-03-06 15:56:53.634

如果你有一个Date对象,

    Date date = new Date();
    long longValue2 = date.getTime();

    LocalDateTime dateTime =
            LocalDateTime.ofInstant(Instant.ofEpochMilli(longValue2), ZoneId.systemDefault());
    String formattedString = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));

    System.out.println(dateTime.toString()); // 2018-03-06T15:59:30.278
    System.out.println(formattedString);     // 2018-03-06 15:59:30.278

如果你只有epoch毫秒,

LocalDateTime date =
        LocalDateTime.ofInstant(Instant.ofEpochMilli(epochLongValue), ZoneId.systemDefault());

其他回答

你只需要在日期格式字符串中添加毫秒字段:

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

SimpleDateFormat的API文档详细描述了格式字符串。

博士tl;

Instant.now()
       .toString()

2016 - 05 - 06 - t23:24:25.694z

ZonedDateTime
.now
( 
    ZoneId.of( "America/Montreal" ) 
)
.format(  DateTimeFormatter.ISO_LOCAL_DATE_TIME )
.replace( "T" , " " )

2016-05-06 19:24:25.694

java.time

在Java 8及以后的版本中,我们有Java。在Java 8及以后版本中内置了时间框架。这些新类取代了麻烦的旧java.util.Date/。日历类。新类的灵感来自于非常成功的Joda-Time框架,该框架作为它的继承者,在概念上类似,但重新构建。由JSR 310定义。由ThreeTen-Extra项目扩展。请参阅教程。

注意java。time具有纳秒级分辨率(小数点后9位),而java.util.Date和Joda-Time具有毫秒级分辨率(小数点后3位)。因此,当格式化为只显示小数点后3位时,可能隐藏了数据。

如果希望从数据中删除任何微秒或纳秒,请截断。

Instant instant2 = instant.truncatedTo( ChronoUnit.MILLIS ) ;

java。time类在解析/生成字符串时默认使用ISO 8601格式。末尾的Z是Zulu的缩写,意思是UTC。

一个瞬间表示UTC时间轴上的一个时刻,分辨率最高可达纳秒。在Java 8中,捕获当前时刻的时间限制在毫秒,而在Java 9中,根据计算机硬件时钟的能力,新的实现可以捕获到纳秒。

Instant instant = Instant.now (); // Current date-time in UTC.
String output = instant.toString ();

2016 - 05 - 06 - t23:24:25.694z

将中间的T替换为空格,Z替换为空,以获得所需的输出。

String output = instant.toString ().replace ( "T" , " " ).replace( "Z" , "" ; // Replace 'T', delete 'Z'. I recommend leaving the `Z` or any other such [offset-from-UTC][7] or [time zone][7] indicator to make the meaning clear, but your choice of course.

2016-05-06 23:24:25.694

因为您不关心是否包含偏移量或时区,所以让“本地”日期-时间与任何特定的位置无关。

String output = LocalDateTime.now ( ).toString ().replace ( "T", " " );

乔达时间

非常成功的Joda-Time库是java的灵感来源。时间框架。建议迁移到java。方便的时候。

ISO 8601格式包括毫秒,是Joda-Time 2.4库的默认格式。

System.out.println( "Now: " + new DateTime ( DateTimeZone.UTC ) );

运行时……

Now: 2013-11-26T20:25:12.014Z

此外,如果需要,你还可以请求以毫秒为单位的秒数:

int millisOfSecond = myDateTime.getMillisOfSecond ();

Java 8中的文档将其命名为“秒的分数”,而在Java 6中则命名为“毫秒”。这使我感到困惑

这里有一个使用Millisecond......显示日期和时间的简单示例

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

public class MyClass{

    public static void main(String[]args){
        LocalDateTime myObj = LocalDateTime.now();
        DateTimeFormatter myFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        String forDate = myObj.format(myFormat);
        System.out.println("The Date and Time are: " + forDate);
    }
}

Ans:

DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
ZonedDateTime start = Instant.now().atZone(ZoneId.systemDefault());
String startTimestamp = start.format(dateFormatter);