2013-02-20 56 views
0

這是類客戶的屬性。日期屬性不適用於類的方法java

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文件的方法, 它有日期出生日期錯誤,我不知道是什麼,包括使其工作

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){ 
} 
+0

參考http://stackoverflow.com/questions/4433198/serializing-java-util-date – Meherzad 2013-02-20 13:09:46

回答

1

嘗試

out.append(dateOfBirth.toString()); 

如果您不喜歡該格式,您可以使用SimpleDateFormat來更改它。 (請參見Documentation

0

您的dateOfBirth是Date類型的對象,您無法將其打印在標準syso中。

我要打印日期使用:dateOfBirth.toString());

相關問題