简体   繁体   中英

How to Set Date to JdateChooser in java?

enter image description here

I used java Jcalender_1.4.jar I have date like this,

String date = "13 Oct 2016";

i want this date set to JdateChooser text box, by using this command JdateChooser.setDate();

how to covert string in to date format ?

Calendar ca = new GregorianCalendar();
String day = ca.get(Calendar.DAY_OF_MONTH) + "";
String month = ca.get(Calendar.MONTH) + 1 + "";
String year = ca.get(Calendar.YEAR) + "";

if (day.length() == 1) {
    day = "0" + day;
}
if (month.length() == 1) {
    month = "0" + month;
}

String dd = year + "-" + month + "-" + day;

Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dd);
jDateChooser1.setDate(date);

try this set date form computer date

You can do it using this easily by SimpleDateFormat command

   String date = "13 Oct 2016";
   java.util.Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
   JdateChooser.setDate(date2);

and also you can use any date format.

The following was first written as an answer to this duplicate question . I thought it would be better to have it here so we have all the answers in one place.

The following should work overall, only the details depend on the format of the dates in the JTable (from the other question). I have assumed that from the JTable you get a string like 03/07/2018 (I am told that this format would be commonplace in Portugal). In this case the following formatter will be fine for parsing it. If the string is in a different format, the formatter will have to be different too.

        DateTimeFormatter dtfFormatador = DateTimeFormatter
                .ofLocalizedDate(FormatStyle.MEDIUM)
                .withLocale(Locale.forLanguageTag("pt-PT"));

        LocalDate data = LocalDate.parse(getData, dtfFormatador);
        Instant instante = data.atStartOfDay(ZoneId.systemDefault()).toInstant();
        Date dateAntiquado = Date.from(instante);
        jdcSeletorDeDatas.setDate(dateAntiquado);

Unfortunately JDateChooser.setDate() requires an old-fashoined Date object, while we'd have preferred to avoid that outdated class. I am using a DateTimeFormatter for parsing the string from the JTable into a LocalDate and converting it to Date . LocalDate is the class from java.time , the modern Java date and time API, that we should use for a date without time of day.

Edit: harsha, your string was

    String date = "13 Oct 2016";

To parse a string in this format, use the following formatter:

    DateTimeFormatter dateFormatter= DateTimeFormatter
            .ofLocalizedDate(FormatStyle.MEDIUM)
            .withLocale(Locale.US);

Otherwise use the code above, where jdcSeletorDeDatas is the JDateChooser .

Link: Oracle tutorial: Date Time explaining how to use java.time .

try {
    String date = "13 Oct 2016";
    Date date2 = new SimpleDateFormat("dd MMM yyyy").parse(date);
    jDateChooser2.setDate(date2);
} catch (Exception e) {
    System.out.println(e);
}
  • Right Click on JDateChooser
  • Go to Properties
  • Change dateFormatString as "dd MMM yyyy"

Try to cast the value to the Date type. This Worked for me:

int SelectRow = jTable1.getSelectedRow();
jDateChooser1.setDate((Date) jTable1.getModel().getValueAt(SelectRow, 2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM