下面的代码给我当前时间。但它没有告诉任何关于毫秒。
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
将正常工作。这里的最低时间单位是秒,但是我如何获得毫秒呢?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

一个 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"));
}
您只需要在日期格式字符串中添加毫秒字段:
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
API doc of SimpleDateFormat详细描述了格式字符串。

试试这个:-
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.htmlDateFormat 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()));
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(59条)