I'm building a very simple expense tracker:
public class ExpenseTracker {
JFrame frame;
JPanel mainPanel;
JPanel inputPanel;
JPanel registerPanel;
DefaultTableModel tableModel;
JDatePicker datePicker;
JTextField nameTextField;
JTextField amountTextField;
SimpleDateFormat sdf;
Date date;
public static void main(String[] args) {
new ExpenseTracker().buildGUI();
}
public void buildGUI() {
mainPanel = new JPanel();
inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
JLabel nameLabel = new JLabel("Name: ");
nameLabel.setFont(new Font("Arial", Font.ITALIC, 25));
nameTextField = new JTextField();
nameTextField.setDocument(new JTextFieldLimit(10));
JLabel dateLabel = new JLabel("Date: ");
dateLabel.setFont(new Font("Arial", Font.ITALIC, 25));
datePicker = new JDatePicker();
JLabel amountLabel = new JLabel("Amount: ");
amountLabel.setFont(new Font("Arial", Font.ITALIC, 25));
amountTextField = new JTextField();
amountTextField.setDocument(new JTextFieldLimit(5));
JButton addExpense = new JButton("Add");
addExpense.addActionListener(new addExpenseListener());
inputPanel.add(nameLabel);
inputPanel.add(nameTextField);
inputPanel.add(dateLabel);
inputPanel.add(datePicker);
inputPanel.add(amountLabel);
inputPanel.add(amountTextField);
inputPanel.add(addExpense);
registerPanel = new JPanel();
tableModel = new DefaultTableModel();
JTable register = new JTable(tableModel);
tableModel.addColumn("Name");
tableModel.addColumn("Date");
tableModel.addColumn("Amount");
register.setBackground(Color.LIGHT_GRAY);
registerPanel.add(register);
mainPanel.add(BorderLayout.NORTH, inputPanel);
mainPanel.add(BorderLayout.CENTER, registerPanel);
frame.add(mainPanel);
frame.setVisible(true);
}
public void addToRegister() {
String name = nameTextField.getText();
sdf = new SimpleDateFormat();
Date dateToFormat = (Date) datePicker.getModel().getValue();
String date = sdf.format(dateToFormat);
String amount = amountTextField.getText();
tableModel.addRow(new Object[] {name, date, amount});
}
Leaving out a bit of code. I get the exception:
class java.util.GregorianCalendar cannot be cast to class java.util.Date (java.util.GregorianCalendar and java.util.Date are in module java.base of loader 'bootstrap')
and I don't know how do I change this. I know the thing is how to get the date from the DatePicker and the class clash there. Using jdatepicker libraries.
CodePudding user response:
java.time
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API.
First, make the following change (check Calendar#getTime
to learn more)
Date dateToFormat = datePicker.getModel().getValue().getTime();
and then switch to java.time
API as follows:
Instant instant = dateToFormat.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
// A sample formatting
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
String formattedDateStr = zdt.format(formatter);
Now, you can use formattedDateStr
in your components.
An alternative way to get ZonedDateTime
can be as suggested by Ole V.V.:
GregorianCalendar selected = (GregorianCalendar) datePicker.getModel().getValue();
ZonedDateTime zdt = selected.toZonedDateTime();
Learn more about the modern Date-Time API from Trail: Date Time.