Hey everybody :)

So im new to programming and im using the jDateChooser from the com.toedter.calendar package

the problem that I am having is that I am not able to get the current date to display ... you have to just select the date yourself ... can anyone give me some help with this

also how do you set another jDateChooser 1 month after the current day?

Many thanks in advance for any help that comes my way :)

Greg Stoltz

Dani AI

Generated

Short answer: initialize the JDateChooser with a Date object and compute "one month later" with java.time (then convert to java.util.Date). 's suggestion to inspect the component's get/set methods is on the right track; the practical, robust pattern is to set the date on the Swing event thread and use java.time for month arithmetic to avoid subtle end-of-month surprises.

Example (run on the EDT; convert LocalDate to Date for JDateChooser):

import javax.swing.SwingUtilities;
import com.toedter.calendar.JDateChooser;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

SwingUtilities.invokeLater(() -> {
    JDateChooser start = new JDateChooser();
    Date today = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
    start.setDate(today);

    JDateChooser plusOneMonth = new JDateChooser();
    LocalDate oneMonthLater = LocalDate.now().plusMonths(1); // handles month-end correctly
    plusOneMonth.setDate(Date.from(oneMonthLater.atStartOfDay(ZoneId.systemDefault()).toInstant()));

    // add components to frame/panel here (or call setDate after initComponents() if using a GUI builder)
});

Troubleshooting notes and cautions:

  • Always update Swing components on the EDT (invokeLater/invokeAndWait). Calling setDate from a background thread can cause no visible change or race conditions.
  • Use java.time (Java 8+) for date math: LocalDate.plusMonths(1) will produce the last valid day of the next month when necessary (e.g., Jan 31 -> Feb 28/29).
  • JDateChooser holds a java.util.Date; if time-of-day matters, set the desired time before converting.
  • If the chooser still appears empty, confirm the code that runs after component creation doesn't overwrite the date (GUI builders sometimes re-initialize components). Also verify the component is a JDateChooser instance (not a JCalendar-only panel) and call setDate after initComponents().

Reference: asked about current/default date and next-month behavior; pointed to inspecting the component—this note shows a straightforward, thread-safe implementation and common pitfalls.

Hello Greg try get/set methods:

//package com.toedter.calendar.JCalendar;
 public static void main(String[] s) {
        JFrame frame = new JFrame("JCalendar");
        JCalendar jcalendar = new JCalendar();
        frame.getContentPane().add(jcalendar);
        frame.pack();
        frame.setVisible(true);
        //1.
        Date date = jcalendar.getDate();
        System.out.println(date);
        //2.
        //second calendar

        JFrame frame2 = new JFrame("JCalendar2");
        JCalendar jcalendar2 = new JCalendar();
        frame2.getContentPane().add(jcalendar2);
        frame2.pack();
        frame2.setVisible(true);
        int month = jcalendar2.getMonthChooser().getMonth();
        System.out.println(month);
        // months calculated from zero
        month++;
        // attention also check month [0,11], no more then 11, change year
        jcalendar2.getMonthChooser().setMonth(month);
        Date date2 = jcalendar2.getDate();
        System.out.println(date2);
    }

Result

run:
Fri Jul 24 01:07:24 CEST 2009
6
Mon Aug 24 01:07:24 CEST 2009

quuba

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.