gpt4 book ai didi

java - 我正在尝试从 DateChooserPanel 获取字符串类型的日期并在文本字段上显示日期

转载 作者:行者123 更新时间:2023-12-02 05:00:09 25 4
gpt4 key购买 nike

我似乎无法从 DateChooserPanel 获取日期...

我想从 DateChooserPanel 中选择一个日期,并且想将所选日期显示到文本字段。

如何从 DateChooserPanel 获取 String 类型的日期?

我尝试将日期格式更改为字符串,但没有成功。

我正确使用日期选择器吗?

错误表明它无法将给定对象格式化为日期...

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.Caret;

import java.awt.BorderLayout;
import datechooser.beans.DateChooserPanel;
import datechooser.beans.DateChooserDialog;
import datechooser.beans.DateChooserCombo;
import java.awt.TextField;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SetTime {

private JFrame frame;
private DateChooserPanel dateChooserPanel_1;
private JTextField textField;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SetTime window = new SetTime();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

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

/**
* Initialize the contents of the frame.
*/
private void initialize() {
this.frame = new JFrame();
this.frame.setBounds(100, 100, 450, 300);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.getContentPane().setLayout(null);

this.dateChooserPanel_1 = new DateChooserPanel();
this.dateChooserPanel_1.setBounds(12, 44, 250, 180);
this.frame.getContentPane().add(this.dateChooserPanel_1);

this.textField = new JTextField();
this.textField.setBounds(306, 113, 116, 21);
this.frame.getContentPane().add(this.textField);
this.textField.setColumns(10);

Calendar date = dateChooserPanel_1.getSelectedDate();

String strdate = DateFormat.getDateInstance().format(date);

textField.setText(strdate);

}
}

最佳答案

您的问题是您尝试将 Calendar 对象传递到需要 Date 对象的方法中。

从日历调用中获取日期

Date selectedDate = date.getTime();

然后就可以使用

String strdate = DateFormat.getDateInstance().format(selectedDate);

关于java - 我正在尝试从 DateChooserPanel 获取字符串类型的日期并在文本字段上显示日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56390881/

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