I have a code where a Student Object is created and depending on its time has to move from one list to another, therefore I need to timestamp those objects to know when they were created
我有一个代码,其中创建了一个学生对象,并且根据它的时间必须从一个列表移动到另一个列表,因此我需要为这些对象添加时间戳以了解它们何时被创建
4
class Student
{
private Date creationDate = new Date();
public Date getCreationDate()
{
return new Date(creationDate.getTime());
}
}
0
Initialize an instance variable with the current time in the constructor.
使用构造函数中的当前时间初始化实例变量。
The code that created it can then interrogate the new instance and move it to the appropriate list.
然后,创建它的代码可以询问新实例并将其移动到适当的列表。
If the creation time only matters once, don't even use an instance variable, just get the current time when you create it and act accordingly.
如果创建时间只重要一次,甚至不使用实例变量,只需在创建时获取当前时间并采取相应措施。
0
System.currentTimeMillis() returns the current time in milliseconds.
System.currentTimeMillis()以毫秒为单位返回当前时间。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/11/10/cc1200d3bc1cca085d32190b07ee1f5f.html。