0

I have following screenshot from jswing application. JswingDate

Here, I have written as such to select date from the date picker -

//CheckIn Date
JDateChooser dateChooser = new JDateChooser();
dateChooser.setBounds(177, 136, 149, 22);
frame.getContentPane().add(dateChooser);

//CheckOut Date
JDateChooser dateChooser_1 = new JDateChooser();
dateChooser_1.setBounds(177, 136, 149, 22);
frame.getContentPane().add(dateChooser_1);

Now, How to write the logic to select the Check In date earlier than Check Out date? If the Check In date is after Check Out date, an error should be popped up saying "check in date should be earlier than checkout date"

I have used action listener but I am not able to replicate.

  • Check the answer provided [here](https://stackoverflow.com/questions/10021565/how-to-show-only-date-after-the-date-of-today-in-jcalendar?noredirect=1&lq=1#answer-10023003) . Is that something you need? – Procrastinator Oct 05 '17 at 09:53
  • 1
    Also, to check whether the check-in date is after the chcek-out date, you can try this: `if(checkInDate.after(checkOutDate)) { // Do your Stuff. }` – Procrastinator Oct 05 '17 at 09:57
  • There are several things you "could" do - you could make the check out field uneditable until the check in field is filled in, at which time you could constrain the date picker to dates after the check in date – MadProgrammer Oct 05 '17 at 10:16

2 Answers2

1

Now, How to write the logic to select the Check In date earlier than Check Out date?

Start by having a look at the JavaDocs for java.util.Date, you will find that it has a number of handy methods, notably before and after. From this you can build a simple logic check based on your needs

If the Check In date is after Check Out date, an error should be popped up saying "check in date should be earlier than checkout date"

The most common method is to use a JOptionPane, take a look at How to make dialogs for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You need to add an ActionListener to dateChooser_1

Read about ActionListeners in this tutorial:

https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Dinh
  • 759
  • 5
  • 16
  • I have used this, but I am not able to replicate because the variable dateChooser and dataChooser_1 belongs to different functions. So, I get error. How to bring both of them together and use them. Say if, I had the date in String format such as, String date1 = "date 1" and String date2 = "date 2", I would have compared and written the code. –  Oct 05 '17 at 09:37
  • Make them a field – Dinh Oct 05 '17 at 09:38
  • That is possible, but I want to use DatePicker jar file to generate the date. Is there anyway we can do it? –  Oct 05 '17 at 09:40
  • Continue reading your books Sir you need to pickup some basics first – Dinh Oct 05 '17 at 09:41
  • @aanshu If you have date values as `String`s, then you are doing something horribly wrong and need to seriously re-think your approach. You should get all values into their appropriate type values, this would mean using `Date` or, preferably, `LocalDate` – MadProgrammer Oct 05 '17 at 10:28
  • Below is the code I tried: 'SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = null; Date d2 = null; try { d1 = format.parse(format.format(dateChooser.getDate())); d2 = format.parse(format.format(dateChooser_1.getDate())); if(d1.after(d2)) { JOptionPane.showMessageDialog(frame, "Check In date should be earlier than Check Out date"); } } catch (Exception e) { e.printStackTrace(); }' –  Oct 05 '17 at 10:35