0

这是类客户的属性。

public class Customer {
private String customer_username;
private String customer_password;
private String name;
private String surname;
private Date dateOfBirth;
private String address;
private String postcode;
private String email;
(...)

这是一种保存到 txt 文件的方法,它的 Date dateOfBirth 有错误,我不知道要包含什么才能使其工作

public void saveToFile(){
    FileWriter out = null;
    try{
        out = new FileWriter("CustomerDetails.txt", true);
        out.append("Customer Username:   ");
        out.append(customer_username);
        out.append(System.getProperty("line.separator"));
        out.append("Customer Password:   ");
        out.append(customer_password);
        out.append(System.getProperty("line.separator"));
        out.append("Name:   ");
        out.append(name);
        out.append(System.getProperty("line.separator"));
        out.append("Surname:   ");
        out.append(surname);         
        out.append(System.getProperty("line.separator"));
        **out.append("Date of Birth:   ");**
        **out.append(dateOfBirth);**
        out.append(System.getProperty("line.separator"));
        out.append("Address:   ");
        out.append(address);
        out.append(System.getProperty("line.separator"));
        out.append("Postcode:   ");
        out.append(postcode);
        out.append(System.getProperty("line.separator"));
        out.append("Email:   ");
        out.append(email);
        out.append(System.getProperty("line.separator"));
        out.append(System.getProperty("line.separator"));
        out.append("**********************************");
        out.append(System.getProperty("line.separator"));
        out.append(System.getProperty("line.separator"));
        out.close();
    }catch (IOException ioe){  
}
4

2 回答 2

1

try

out.append(dateOfBirth.toString());

If you don't like the format, you can use a SimpleDateFormat to change it. (see the Documentation)

于 2013-02-20T13:05:18.793 回答
0

Your dateOfBirth is an object of type Date, you can not print it in a standart syso.

I you want to print the date use: dateOfBirth.toString());

于 2013-02-20T13:06:49.667 回答