Java 如何在Android中获取当前时间戳,而不会像时钟一样更新

Java 如何在Android中获取当前时间戳,而不会像时钟一样更新,java,android,timestamp,Java,Android,Timestamp,我试图在android中获取时间戳,这是我的代码 public String getTimestamp() { String timestamp = DateFormat.getDateTimeInstance().format(new Date()); return timestamp; } 当我创建一个变量来显示时间戳时 Ex)字符串时间戳=getTimestamp() 这应该将当前时间戳保存为字符串。然而,当我打印这个字符串“timestamp”时,它会像时钟一样实时更新

我试图在android中获取时间戳,这是我的代码

public String getTimestamp() {
    String timestamp = DateFormat.getDateTimeInstance().format(new Date());
    return timestamp;
}
当我创建一个变量来显示时间戳时

Ex)字符串时间戳=getTimestamp()

这应该将当前时间戳保存为字符串。然而,当我打印这个字符串“timestamp”时,它会像时钟一样实时更新。我只想要当前的时间戳。我没有把它放在for循环或任何东西中,在那里它可以被更新

这里是打印出来的地方:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rootView = inflater.inflate(R.layout.single_list_item, parent, false);
    TextView nameView = (TextView) rootView.findViewById(R.id.idView);

    GeLoBeacon beacon = beacons.get(position);

    if (!beaconsFound.contains(Integer.toString(beacon.getBeaconId()))) {
        beaconsFound.add(Integer.toString(beacon.getBeaconId()));
    }
    String times = getTimestamp();
    nameView.setText(times);
    return rootView;
}
给你:

public String getTimestamp() {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
      return sdf.format(new Date());
}

更改格式(“yyyy-MM-dd'T'HH:MM:ss”)并以该特定格式获取您的日期

将timeStamp定义为字段变量,然后使用setTimeStamp(){//分配当前时间}和gettimestamp(){返回timeStamp;)读取时间戳值。

您的方法getView()在适配器的每个项上调用,因此在每个项上设置了不同的时间。在调用方法listView.setAdapter()之前,将timestamp设置为适配器中的字段变量一旦返回的字符串无法更新,请显示打印此字符串的代码string@Max77我在打印出来的地方添加了代码。这只是格式化时间戳的另一种方式,不能解决更新时间戳的问题。