我想写一个Account类,并在该类被实例化时,得到创建时间,下面是我的想法,但很显然,在没有调用toString时,时间时并未被初始化的,请大神帮忙解答 import java.util.Date;
import java.text.SimpleDateFormat; public class Account {
private String id;
private double balance;
private double annualInteresRate;
final private SimpleDateFormat dateCreate;

public Account() {
this.id = "0";
this.balance = 0;
this.annualInteresRate = 0;
dateCreate = new SimpleDateFormat("yyyy-mm");

}

public Account(String id, double balance,double annualInteresRate) {
this.id = id;
this.balance = balance;
this.annualInteresRate = annualInteresRate;
dateCreate = new SimpleDateFormat("yyyy-mm");
dateCreate.format(new Date());
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public double getBalance() {
return balance;
} public void setBalance(double balance) {
this.balance = balance;
} public double getAnnualInteresRate() {
return annualInteresRate;
} public void setAnnualInteresRate(double annualInteresRate) {
this.annualInteresRate = annualInteresRate;
} public SimpleDateFormat getDateCreate() {
return dateCreate;
}
public void withDraw(double money) {
balance -= money;
}

public void deposit(double money) {
balance += money;
}

public double getMonthlyInterest() {
return balance * (annualInteresRate / 100 / 12);
}

@Override
public String toString() {
return "当前账户id: " + getId() + "当前余额: " + getBalance()
+ "当前年利率: " + getAnnualInteresRate() + "账户创建日期: " + dateCreate.format(new Date());
} }

解决方案 »

  1.   

    表达清楚一点。。还有是MM才是月不是mm...
      

  2.   

    看到yyyy-mm我的强迫症就犯了
      

  3.   

    写了个小例子class User { private String name;
    private Integer age;
    private String date; public User() {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    this.date = simpleDateFormat.format(new Date());
    } // 省略了name和age的get/set方法
    // 对date属性只设置get方法不允许修改

    public String getDate() {
    return date;
    } @Override
    public String toString() {
    return "User [name=" + name + ", age=" + age + ", date=" + date + "]";
    }}
      

  4.   

    构造函数里new Date()就好了,要格式的话用个SimpleDateFormat parse一下。
      

  5.   

    对于这种有点好的建议是:使用系统当前的时间戳: System.currentTimeMillis(),这种存起来也方便,抓换格式也方便
      

  6.   

    将 new SimpleDateFormat("yyyy-mm"); 放在构造函数也是无语了,为什么不直接声明为 final?包括 Date 也是,而是运行时每次获取。
      

  7.   

    private final Date dt = new Date();
    private final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    private final String dts = df.format(dt); public Account() {
    this("0", 0.0, 0.0);
    } public Account(String id, double balance, double annualInteresRate) {
    this.id = id;
    this.balance = balance;
    this.annualInteresRate = annualInteresRate;
    } public String getDateCreate() {
    return dts;
    } @Override
    public String toString() {
    return "当前账户id:" + getId() + "当前余额:" + getBalance() + "当前年利率:" + getAnnualInteresRate() + "账户创建日期:" + dts;
    }
      

  8.   

    把你的final private SimpleDateFormat dateCreate;
    改成private static SimpleDateFormat dateCreate = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
    然后加一个变量private  String currentTime;
    并在构造中加
    this.currentTime = dateCreate.format(new Date());
    最后
    @Override
    public String toString() {
    return "当前账户id: " + getId() + "当前余额: " + getBalance()
    + "当前年利率: " + getAnnualInteresRate() + "账户创建日期: " + currentTime ;
    }