我想要得到当前的时间戳: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();
当前回答
解决方案是:
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
其他回答
1320917972是从1970年1月1日00:00:00 UTC开始使用秒数的Unix时间戳。您可以使用TimeUnit类进行单位转换——从System.currentTimeMillis()转换为秒。
String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
解决方案是:
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());
这里是最广为人知的方法的比较列表
Kotlin解决方案:
val nowInEpoch = Instant.now().epochSecond
确保你的最低SDK版本是26。