Bra*_*ein 0 java formatting time android
在我的应用程序中,我有一个textView,其中包含来自我的应用程序的实时消息,当事情发生时,消息将打印到此文本框中.每条消息都带有HH:MM:SS的时间戳.
到目前为止,我一直在追逐看起来像是内存泄漏,但事实证明,这只是我的时间戳格式化方法(见下文),它显然产生了数千个后来获得gc'd的对象.对于每秒1-10条消息,我看到GC每秒收集500k-2MB垃圾,而这种方法已经到位.删除它后,不再有垃圾问题(它回到大约30秒的漂亮间隔,通常只有几k垃圾)
所以我正在寻找一种新的,更轻量级的方法来生成HH:MM:SS时间戳字符串:)
旧代码:
/**
* Returns a string containing the current time stamp.
* @return - a string.
*/
public static String currentTimeStamp() {
String ret = "";
Date d = new Date();
SimpleDateFormat timeStampFormatter = new SimpleDateFormat("hh:mm:ss");
ret = timeStampFormatter.format(d);
return ret;
}
Run Code Online (Sandbox Code Playgroud)