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

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可以很好地工作。这里最低的时间单位是秒,但我怎么也能得到毫秒呢?


当前回答

博士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 ();

其他回答

为了补充上述答案,这里有一个小的工作示例程序,打印当前时间和日期,包括毫秒。

import java.text.SimpleDateFormat;
import java.util.Date;

public class test {
    public static void main(String argv[]){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date now = new Date();
        String strDate = sdf.format(now);
        System.out.println(strDate);
    }
}

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

Java一行程序

public String getCurrentTimeStamp() {
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
}

JDK8风格

public String getCurrentLocalDateTimeStamp() {
    return LocalDateTime.now()
       .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
}

博士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 ();