How to extract and reset date from JDatePicker?

8.2k views Asked by At

I have used code from this post to get jdatepicker into my simple program (below is only code needed for jdatepicker). It works greate, but I have 2 questions:

    1. How to get selected date to String?
    2. How to reset date in jdatepicker textbox?
//imports

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import javax.swing.JFormattedTextField.AbstractFormatter;

import org.jdatepicker.*;
import org.jdatepicker.impl.*;
import org.jdatepicker.util.*;

------------------------------------------------------------------------
//constructor

UtilDateModel model = new UtilDateModel();
    Properties p = new Properties();
    p.put("text.today", "today");
    p.put("text.month", "month");
    p.put("text.year", "year");
    JDatePanelImpl datePanel = new JDatePanelImpl(model,p);
    JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());
    datePicker.setBounds(230, 30, 110, 30);
    add(datePicker);

------------------------------------------------------------------------
//dateformater

public class DateLabelFormatter extends AbstractFormatter
    {

        private String datePattern = "yyyy-MM-dd";
        private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

        @Override
        public Object stringToValue(String text) throws ParseException
        {
            return dateFormatter.parseObject(text);
        }

        @Override
        public String valueToString(Object value) throws ParseException
        {
            if (value != null)
            {
                Calendar cal = (Calendar) value;
                return dateFormatter.format(cal.getTime());
            }

            return "";
        }

    }
1

There are 1 answers

0
mohamed stitane On
  1. How to get selected date to String?

    datePicker.getJFormattedTextField().getText();

  2. How to reset date in JDatePicker textbox?

    datePicker.getJFormattedTextField().setText("");