gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:49:02 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com