2014-02-27 27 views
1

我想獲取的日期將採用日期格式YYYY-MM-DD HH:MM:DD:MS 例如 2009-13-01 10:22:33:44如何從getDateTimeInstance獲取ms

,我沒有成功獲得MS

public static DateFormat getFormat() { 
    String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$ 
    Locale locale = new Locale(systemLocale); 
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale); 
    return dateFormat; 
} 

String pattern = "/Date\\((\\d+)\\)/"; //$NON-NLS-1$ 
String updated = (strValue).replaceAll(pattern, "$1"); //$NON-NLS-1$ 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(Long.parseLong(updated)); 
     return getFormat().format(new Date(calendar.getTimeInMillis())); 

我想也到日期格式更改爲FULL和長,問題也沒有工作

回答

2

你可以這樣做使用SimpleDateFormat。使用'S'毫秒。

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); 
Date now = new Date(); 
System.out.println(df.format(now)); 

打印:

2014-02-27 08:45:25:093 

在你的代碼,你可以修改getFormat()功能:

public static DateFormat getFormat() { 
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); 
} 

(注意,這會返回一個固定的日期格式,這是不依賴語言環境了)。

+0

我需要在哪裏替換我的代碼中的SimpleDateFormat? – user1365697

+0

修改您的'getFormat()'函數以返回'SimpleDateFormat'的一個實例。 – Grodriguez

+0

不需要,SimpleDateFormat繼承自DateFormat,@Grodriguez – hd1

2
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
+0

我需要在哪裏替換我的代碼中的SimpleDateFormat? – user1365697

+0

返回新的SimpleDateFormat(「yyyy-MM-dd HH:mm:ss.SSS」);從getFormat()方法@ user1365697 – Sumeet

1

如果你知道確切的格式,爲什麼不嘗試用格式字符串指定它(在我看來,秒和毫秒之間的冒號字符不是「非常」的標準)?

yyyy-MM-dd HH:mm:ss:SSS 

把它放在SimpleDateFormat一個實例,在這裏你去。

只是說明:你的例子不適合你的格式:13不能是一個有效的月份。