Can not retrieve date from JDateChooser in Java

て烟熏妆下的殇ゞ 提交于 2019-12-11 02:01:54

问题


I am using JDateChooser from here

However I can not retrieve the date in format set with the method setDateFormatString while displaying it in console.

In first label shows actual date retrieved from JDateChooser while second label shows the format which I have set. When I select date from JDateChooser I get the date as 22-07-2011 as shown in image. But when I use getDate method, it will give me date as Fri Jul 22 00:00:00 GMT+05:30 2011. I want only 22-07-2011.

Here is my code. I am using Netbeans IDE 7.0

public JDateChooser() {
    initComponents();
    dateChooser.setDateFormatString("dd-MM-yyyy");
}
private void btnDisplayDateActionPerformed(java.awt.event.ActionEvent evt) {
    String dateFromDateChooser = dateChooser.getDate().toString();
    lblDate.setText(dateFromDateChooser);
    lblDateFormat.setText(dateChooser.getDateFormatString().toString());
    System.out.println(dateFromDateChooser);
}
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new JDateChooser().setVisible(true);
        }
    });
}

回答1:


You can use the String.format method to get the desired result.

Date dateFromDateChooser = dateChooser.getDate();
String dateString = String.format("%1$td-%1$tm-%1$tY", dateFromDateChooser);



回答2:


Try this

java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("dd-MM-yyyy");
String formattedDate = fmt.format(dateChooser.getDate());



回答3:


there are two choises

1/ you already dowloaded code source

    calDealDate.setPreferredSize(new Dimension(90, 23));
    calDealDate.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
    calDealDate.setSpiningCalendarField(Calendar.DAY_OF_MONTH);
    calDealDate.setFont(new Font("SansSerif", Font.BOLD, 12));
    calDealDate.setBackground(AppVariables.fieldColor);
    calDealDate.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            //here is your date depends of previous setings
        }
    });
    calDealDate.setToolTipText(" some tooltip text  ");

2/ you already dowloaded jar file, then you have to find this method or equivalent from API

EDIT: maybe I would be preffered to start with code source, just import that into project




回答4:


Try This one

   public void calendarExpale(){
    JPanel content = new JPanel();
    content.setLayout(new FlowLayout());
    final JTextField textField = new JTextField(15);
    JDateChooser chooser = new JDateChooser();

   chooser.setSize(15, 10);
  chooser.addPropertyChangeListener("date", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {

    JDateChooser chooser = (JDateChooser)evt.getSource();
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:MM:SS");
    textField.setText(formatter.format(chooser.getDate()));
}
  });

  content.add(textField);
    content.add(chooser);
}



回答5:


Use String Format Method, it will help you and how to use it see this

https://www.youtube.com/watch?v=FEM0BpZSXmU

this link has same to same tutorial as you want.



来源:https://stackoverflow.com/questions/6760690/can-not-retrieve-date-from-jdatechooser-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!