我想要得到当前的时间戳:1320917972
int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts = tsTemp.toString();
我想要得到当前的时间戳:1320917972
int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts = tsTemp.toString();
当前回答
这里是最广为人知的方法的比较列表
其他回答
使用以下方法获取当前时间戳。这对我来说很有效。
/**
*
* @return yyyy-MM-dd HH:mm:ss formate date as string
*/
public static String getCurrentTimeStamp(){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateTime = dateFormat.format(new Date()); // Find todays date
return currentDateTime;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
解决方案是:
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
它的用法很简单:
long millis = new Date().getTime();
如果你想在特定的格式,那么你需要像下面这样的Formatter
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String millisInString = dateFormat.format(new Date());
这里是最广为人知的方法的比较列表
你可以通过尝试下面的代码在Android中获得当前时间戳
time.setText(String.valueOf(System.currentTimeMillis()));
和时间戳到时间格式
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(new Date(Long.parseLong(time.getText().toString())));
time.setText(dateString);