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

下面的代码为我提供了当前时间。 但这并不能说明毫秒。

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;

}

我以2009-09-22 16:47:08 (YYYY-MM-DD HH:MI:Sec)的格式获取日期。

但是我想以2009-09-22 16:47:08.128 ((YYYY-MM-DD HH:MI:Sec.Ms))的格式检索当前时间-其中128表示毫秒。

SimpleTextFormat将正常工作。 在这里,最小的时间单位是秒,但是如何获得毫秒呢?

#1楼

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

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

#2楼

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

#3楼

尝试这个:-

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");

Date date = new Date();

System.out.println(dateFormat.format(date));

要么

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

Calendar cal = Calendar.getInstance();

System.out.println(dateFormat.format(cal.getTime()));

#4楼

tl; dr

Instant.now()

.toString()

2016-05-06T23: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 8和更高版本中内置了java.time框架。 这些新类取代了麻烦的旧java.util.Date/.Calendar类。 新课程的灵感取自于成功的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 。

Instant代表UTC时间线上的一个时刻,分辨率最高可达纳秒 。 Java 8中捕获当前时刻的时间限制为毫秒 ,而Java 9中的新实现则可以捕获纳秒的时间,具体取决于计算机硬件时钟的能力。

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

String output = instant.toString ();

2016-05-06T23: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.time框架的灵感来源。 建议在方便时迁移到java.time。

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

#5楼

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值