尝试从 jcalendar 获取日期时出现 DateTimeParseException

I'm getting DateTimeParseException when trying to get date from jcalendar

我需要以年、月、日和秒为单位计算年龄,我正在使用 jcalendar 获取用户输入,但我一直收到 DateTimeParseException 错误,有人可以告诉我如何解决这个问题。

    JDateChooser dateChooser = new JDateChooser();
    dateChooser.setDateFormatString("yyyy-MM-dd HH:mm:ss");
    dateChooser.setBounds(419, 89, 175, 20);
    frame.getContentPane().add(dateChooser);

    final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());

    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime startDate = LocalDateTime.parse(start ,format);
    LocalDateTime endDate = LocalDateTime.now();

    JButton btnNewButton = new JButton("Years");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        JOptionPane.showMessageDialog(null,  ChronoUnit.YEARS.between(startDate, endDate));
             }
    });`

这是错误。

java.time.format.DateTimeParseException: Text '' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)

完整代码

 import java.awt.EventQueue;
 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 import javax.swing.JTextField;
 import javax.swing.JButton;
 import javax.swing.JLabel;
 import javax.swing.RootPaneContainer;
 import java.awt.Font;  
 import javax.swing.JList;
 import javax.swing.JPasswordField;
 import javax.swing.JComboBox;
 import javax.swing.plaf.RootPaneUI;
 import com.toedter.calendar.JCalendar;
 import com.toedter.calendar.JDateChooser;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import java.awt.im.InputContext;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.text.ParseException;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.time.temporal.ChronoUnit;
 import com.toedter.components.JLocaleChooser;
 import java.util.Date;
 import java.util.Locale;
 import java.awt.event.InputMethodListener;
 import java.awt.event.InputMethodEvent;
 public class Frame1 {



private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
                Frame1 window = new Frame1();
                window.frame.setVisible(true);

        }
    });
}

/**
 * Create the application.
 */
public Frame1() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */

private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setLocale(Locale.US);
    frame.setBounds(100, 100, 901, 623);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);



    JDateChooser dateChooser = new JDateChooser();
    dateChooser.setDateFormatString("yyyy-MM-dd HH:mm:ss");
    dateChooser.setBounds(419, 89, 175, 20);
    frame.getContentPane().add(dateChooser);

    final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());


    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime startDate = LocalDateTime.parse(start ,format);
    LocalDateTime endDate = LocalDateTime.now();

     JButton btnNewButton = new JButton("Years");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        JOptionPane.showMessageDialog(null,  ChronoUnit.YEARS.between(startDate, endDate));
            }
    });
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 18));
    btnNewButton.setBounds(55, 366, 200, 52);
    frame.getContentPane().add(btnNewButton);


    JButton btnNewButton_1 = new JButton("Months");
    btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 18));
    btnNewButton_1.setBounds(326, 367, 200, 50);
    frame.getContentPane().add(btnNewButton_1);

    JButton btnNewButton_2 = new JButton("Days");
    btnNewButton_2.setFont(new Font("Tahoma", Font.PLAIN, 18));
    btnNewButton_2.setBounds(590, 367, 200, 50);
    frame.getContentPane().add(btnNewButton_2);

    JButton btnNewButton_3 = new JButton("Hours");
    btnNewButton_3.setFont(new Font("Tahoma", Font.PLAIN, 18));
    btnNewButton_3.setBounds(55, 472, 200, 50);
    frame.getContentPane().add(btnNewButton_3);

    JButton btnNewButton_4 = new JButton("Seconds");
    btnNewButton_4.setFont(new Font("Tahoma", Font.PLAIN, 18));
    btnNewButton_4.setBounds(326, 472, 200, 50);
    frame.getContentPane().add(btnNewButton_4);

    JButton btnNewButton_5 = new JButton("Milliseconds");
    btnNewButton_5.setFont(new Font("Tahoma", Font.PLAIN, 18));
    btnNewButton_5.setBounds(590, 472, 200, 50);
    frame.getContentPane().add(btnNewButton_5);

    JLabel lblNewLabel = new JLabel("calculate your age...");
    lblNewLabel.setFont(new Font("Tahoma", Font.ITALIC, 22));
    lblNewLabel.setBounds(310, 268, 403, 50);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblVoerJeGeboorte = new JLabel("insert age");
    lblVoerJeGeboorte.setFont(new Font("Tahoma", Font.ITALIC, 16));
    lblVoerJeGeboorte.setBounds(226, 83, 272, 26);
    frame.getContentPane().add(lblVoerJeGeboorte);
    }
 }

解析日期时,您需要处理异常

将您用来解析日期 or/and 时间的代码放在 try..catch 块中,它不会向您显示错误!

try{
    dateChooser.setDateFormatString("yyyy-MM-dd HH:mm:ss");
    dateChooser.setBounds(419, 89, 175, 20);
    frame.getContentPane().add(dateChooser);

    final String start = (((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());

    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime startDate = LocalDateTime.parse(start ,format);
    LocalDateTime endDate = LocalDateTime.now();
}
catch(ParseException e){
    e.printStackTrace();
}