我想要得到当前的时间戳:1320917972

int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String ts =  tsTemp.toString();

当前回答

解决方案是:

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();

其他回答

解决方案是:

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();

你可以通过尝试下面的代码在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);

我建议使用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;
    }

来自开发者博客:

System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.

1320917972是从1970年1月1日00:00:00 UTC开始使用秒数的Unix时间戳。您可以使用TimeUnit类进行单位转换——从System.currentTimeMillis()转换为秒。

String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));