我想要得到当前的时间戳: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();
当前回答
Kotlin解决方案:
val nowInEpoch = Instant.now().epochSecond
确保你的最低SDK版本是26。
其他回答
你可以使用SimpleDateFormat类:
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
String format = s.format(new Date());
我建议使用Hits的答案,但添加一个Locale格式,这是如何Android 开发人员建议:
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
return dateFormat.format(new Date()); // Find todays date
} catch (Exception e) {
e.printStackTrace();
return null;
}
你可以通过尝试下面的代码在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);
Kotlin解决方案:
val nowInEpoch = Instant.now().epochSecond
确保你的最低SDK版本是26。
它的用法很简单:
long millis = new Date().getTime();
如果你想在特定的格式,那么你需要像下面这样的Formatter
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String millisInString = dateFormat.format(new Date());