JAVA -从JDatePicker获取日期-类JAVA .util. gregoriancalendar不能强制转换为



我正在构建一个非常简单的费用跟踪器:

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});
}

省略了一些代码。我得到了异常:

类java.util.GregorianCalendar不能强制转换为类java.util.Date (java.util。在GregorianCalendar和java.util.Date中java模块。加载器'bootstrap'的基础)

我不知道如何改变它。我知道这个是如何从DatePicker和那里的类冲突中获取日期。使用jdatepicker库

java.time

java.utilDate-Time API及其格式化APISimpleDateFormat过时且容易出错。建议完全停止使用它们并切换到现代Date-Time API。

首先,进行以下更改(检查Calendar#getTime以了解更多信息)

Date dateToFormat = datePicker.getModel().getValue().getTime();

,然后切换到java.timeAPI如下:

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);

现在,你可以在你的组件中使用formattedDateStr

获得ZonedDateTime的另一种方法可以由Ole v.v.建议:

GregorianCalendar selected = (GregorianCalendar) datePicker.getModel().getValue();
ZonedDateTime zdt = selected.toZonedDateTime();

Trail: Date Time了解更多关于现代Date-Time API的信息.

相关内容

  • 没有找到相关文章

最新更新