Reputation: 13
I'm making a GUI in java for a todo list and i want to write a todo type object in a file. A todo object has name, description and a date that determinds this to be done. With GUI a user can add as many todo objects he wants with them be written to a file. I'm having trouble getting the date from user from a JTextField using getText() and parsing it in a LocalDate type object in order to be written in the file.
Code I'm running now:
//registration method
public void RegularObligationRegister(RegularObligations R) throws IOException {
try {
objOutObligations.writeObject(R);
JOptionPane.showMessageDialog(null, "Save Success");
System.out.println(R);
objOutObligations.flush();
} catch (FileNotFoundException ex) {
System.out.println("Error with specified file . . .");
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//actionperformed method
@Override
public void actionPerformed(ActionEvent ae) {
.
.
.
if (ae.getSource().equals(REntryObligationSave)) {
RegularObligations R = new RegularObligations(RegularEntry_TextObligationName.getText(),
RegularEntry_TextObligationDescription.getText(),
LocalDate.parse(RegularEntry_TextObligationDeadline.getText()));
try {
this.RegularObligationRegister(R);
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
RegularEntry_TextObligationName.setText(null);
RegularEntry_TextObligationDescription.setText(null);
RegularEntry_TextObligationDeadline.setText(null);
}
Upvotes: 0
Views: 445
Reputation: 101
You need to make sure that your text must be valid LocalDate.parse() method to parse. You need to specify your text format for the LocalDate. For ex: (dd MMM uuuu). Then you should pass your text and formatter to the LocalDate.parse() method as parameter. The example below will help you to parse a String similar to : "31 Dec 2018" as a LocalDate
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM uuuu");
LocalDate.parse(RegularEntry_TextObligationDeadline.getText(),formatter));
You can check DateTimeFormatter to customize your datetype as a text DateTimeFormatter
Upvotes: 3