I'm trying to get value from JDateChooser
and use it as a filename I've created a file with a path, I can write on it, but the only problem is I can't change its name to the variable(data from JDateChooser
)
Here is the part of the code:
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
JDateChooser day = new JDateChooser();
try{
File remindFile = new File("\\path", day + ".txt");
remindFile.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(remindFile));
writer.write(text);
writer.close();
}
catch(Exception k)
{ System.out.println("Oops");}
textField.setText(null);
}
});
btnSave.setFont(new Font("Tahoma", Font.PLAIN, 13));
btnSave.setBounds(401, 215, 108, 30);
panel.add(btnSave);
In the result created file gets name as:
com.toedter.calendar.JDateChooser
[JDateChooser,0,0,0x0,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
How can I fix it?
You are actually appending the JDateChooser
object itself, instead of a String
representation of its selected Date
:
File remindFile = new File("C:\\Users\\student_ib\\eclipse\\d", day + ".txt");
Try :
Date chosenDate = day.getDate();
DateFormat dateFormat = new SimpleDateFormat("yyMMdd");
File remindFile = new File("C:\\Users\\student_ib\\eclipse\\d", dateFormat.parse(chosenDate) + ".txt");
您必须调用day.getDate().getDay()
从JDateChooser
获取日期,而不要使用day
本身。
Check the documentation for JDateChooser
.
You will need to first get the date value via either .getDate()
or .getCalendar()
, then you should use something like SimpleDateFormat
to format the date.
Thank you all for your answers. Using your advices I solved it in this way:
int day = calendar_1.getDayChooser().getDay();
int month = calendar_1.getMonthChooser().getMonth();
int year = calendar_1.getYearChooser().getYear();
String name = "" + day + month + year;
File remindFile = new File(name + ".txt");
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.