当我输入无效日期或除了设定的标准值以外的其他任何东西(yyyy-MM-dd HH:mm:ss)时,我试图这样做我收到错误,告诉我我&#39 ;输入无效的输入。我现在获得的实际结果是它会引发异常。
public void actionPerformed(ActionEvent e) {
final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
final String start_1 = (((JTextField)dateChooser_1.getDateEditor().getUiComponent()).getText());
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startDate = LocalDateTime.parse(start ,format);
LocalDateTime endDate = LocalDateTime.now();
--- datechooser代码
JDateChooser dateChooser = new JDateChooser();
dateChooser.setDateFormatString("yyyy-MM-dd HH:mm:ss");
dateChooser.setBounds(71, 112, 175, 20);
frame.getContentPane().add(dateChooser);
---全班代码
公共课查看{
控制器bereken = new Controller(); 私有JFrame框架;
public void Programma() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
View window = new View();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public View() {
Initialize();
}
private void Initialize() {
frame = new JFrame();
frame.getContentPane().setLocale(Locale.US);
frame.setBounds(100, 100, 751, 523);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JDateChooser dateChooser = new JDateChooser();
dateChooser.setDateFormatString("yyyy-MM-dd HH:mm:ss");
dateChooser.setBounds(71, 112, 175, 20);
frame.getContentPane().add(dateChooser);
JDateChooser chooser = new JDateChooser();
chooser.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("date".equals(e.getPropertyName())) {
System.out.println(e.getPropertyName()
+ ": " + (Date) e.getNewValue());
}
}
});
JButton btnNewButton = new JButton("Years");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
JOptionPane.showMessageDialog(null, bereken.berekenVerschilInJaren(start)+" Jaar");
}
});
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnNewButton.setBounds(259, 391, 173, 52);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Months");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
JOptionPane.showMessageDialog(null, bereken.berekenVerschilInMaanden(start)+" Maanden");
}
});
btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnNewButton_1.setBounds(62, 392, 173, 50);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Days");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
JOptionPane.showMessageDialog(null, bereken.berekenVerschilInDagen(start)+" Dagen");
}
});
btnNewButton_2.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnNewButton_2.setBounds(259, 330, 173, 50);
frame.getContentPane().add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("Hours");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
JOptionPane.showMessageDialog(null, bereken.berekenVerschilInUren(start)+" Uren");
}
});
btnNewButton_3.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnNewButton_3.setBounds(62, 330, 173, 50);
frame.getContentPane().add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("Seconds");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
JOptionPane.showMessageDialog(null, bereken.berekenVerschilInSeconden(start)+" Seconden");
}
});
btnNewButton_4.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnNewButton_4.setBounds(257, 267, 175, 50);
frame.getContentPane().add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("Milliseconds");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
JOptionPane.showMessageDialog(null, bereken.berekenVerschilInMilliseconden(start)+" milliseconden");
}
});
btnNewButton_5.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnNewButton_5.setBounds(62, 267, 173, 50);
frame.getContentPane().add(btnNewButton_5);
JLabel lblNewLabel = new JLabel("Your selected Primary date in:");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblNewLabel.setBounds(64, 211, 403, 50);
frame.getContentPane().add(lblNewLabel);
JLabel lblVoerJeGeboorte = new JLabel("Select Primary Date:");
lblVoerJeGeboorte.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblVoerJeGeboorte.setBounds(74, 83, 272, 26);
frame.getContentPane().add(lblVoerJeGeboorte);
JDateChooser dateChooser_1 = new JDateChooser();
dateChooser_1.setDateFormatString("yyyy-MM-dd HH:mm:ss");
dateChooser_1.setBounds(502, 112, 175, 20);
frame.getContentPane().add(dateChooser_1);
JLabel label = new JLabel("Select Secondary Date:");
label.setFont(new Font("Tahoma", Font.PLAIN, 16));
label.setBounds(510, 83, 272, 26);
frame.getContentPane().add(label);
JButton btnNewButton_6 = new JButton("Calculate Difference");
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
final String start_1 = (((JTextField)dateChooser_1.getDateEditor().getUiComponent()).getText());
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startDate = LocalDateTime.parse(start ,format);
LocalDateTime endDate = LocalDateTime.now();
long Datum_1 = ChronoUnit.YEARS.between(startDate, endDate);
long Datum_2 = ChronoUnit.DAYS.between(startDate, endDate);
long Datum_3 = ChronoUnit.HOURS.between(startDate, endDate);
long Datum_4 = ChronoUnit.SECONDS.between(startDate, endDate);
long Datum_5 = ChronoUnit.MILLIS.between(startDate, endDate);
DateTimeFormatter format_1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startDate_1 = LocalDateTime.parse(start_1 ,format_1);
LocalDateTime endDate_1 = LocalDateTime.now();
long Datum_6 = ChronoUnit.YEARS.between(startDate_1, endDate_1);
long Datum_7 = ChronoUnit.DAYS.between(startDate_1, endDate_1);
long Datum_8 = ChronoUnit.HOURS.between(startDate_1, endDate_1);
long Datum_9 = ChronoUnit.SECONDS.between(startDate_1, endDate_1);
long Datum_10 = ChronoUnit.MILLIS.between(startDate_1, endDate_1);
long Jaren = Datum_6 - Datum_1;
long Dagen = Datum_7 - Datum_2;
long Uren = Datum_8 - Datum_3;
long Seconden = Datum_9 - Datum_4;
long milliseconden = Datum_10 - Datum_5;
JOptionPane.showMessageDialog(null, Jaren+ " Jaar, "+ Dagen+ " Dagen, "+ Uren+ " Uren, "+ Seconden+ " Seconden, "+ milliseconden+ " milliseconden");
}
});
btnNewButton_6.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnNewButton_6.setBounds(280, 83, 187, 67);
frame.getContentPane().add(btnNewButton_6);
}
}
---控制台结果
线程中的异常" AWT-EventQueue-0" java.time.format.DateTimeParseException:Text' dsadasda'无法在索引0处解析 at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source) at java.time.format.DateTimeFormatter.parse(Unknown Source) at java.time.LocalDateTime.parse(Unknown Source) 在View $ 9.actionPerformed(View.java:164) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.DefaultButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) at java.awt.Component.processMouseEvent(Unknown Source) 在javax.swing.JComponent.processMouseEvent(未知来源) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 在java.awt.EventQueue.access $ 500(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 4.run(未知来源) 在java.awt.EventQueue $ 4.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 在java.awt.EventDispatchThread.run(未知来源)