简体   繁体   中英

Can I have the time of an object from its creation or instance?

我有一个创建学生对象的代码,并且必须根据其时间从一个列表移动到另一个列表,因此我需要给这些对象打上时间戳以了解它们的创建时间。

class Student
{
    private Date creationDate = new Date();

    public Date getCreationDate()
    {
        return new Date(creationDate.getTime());
    }
}

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.

System.currentTimeMillis()返回当前时间(以毫秒为单位)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM