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

标签 java jcalendar datechooser

我需要以年、月、日和秒为单位计算年龄,我使用 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);
    }
 }

最佳答案

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

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

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

关于java - 尝试从 jcalendar 获取日期时出现 DateTimeParseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28925957/

相关文章:

Java向JCalendar添加图标

apache-flex - DateChooser ..禁用年份和月份?

java - 如何使用Sqlite数据库在java中的两个日期之间进行搜索

java - 你可以将 string.split 数组转换为 JButtons

java - 无法实现构造函数(隐式 super 构造函数 Item() 未定义)

java - 用于设置 privateFields 的 yamlbeans 配置

java - 在 JAVA 中格式化 Date.toString() 输出

java - 当我的框架显示时,如何以编程方式打开 DateChooserPanel

Java 扫描器只在方法的第一行运行,为什么不在中间行运行