-1

I am stuck in a programming issue. I am am making Java Application in Eclipse attached with SQlite. I want such state where user choose a date from JDateChooser and date of after two days from the specified one in JDateChooser show into next field. I am using the following code which I know works..

 DateFormat df=new SimpleDateFormat("dd-MM-yyyy");
 Date date =  df.parse(ArrivalDate);
 Calendar cal = Calendar.getInstance();                 
 cal.add(Calendar.DAY_OF_MONTH, 2); // Add 30 days
 Date futureDate = cal.getTime();
 String NextDate=df.format(futureDate);

----Here ArrivalDate is date entered by user. Issue over here is this, that Calender class choose the present date of the day. Not work with the date choose by the user. For example, if today date is 01/08/2018 and user has entered 12/08/2018.. This code will give 03/08/2018 in return, not the 14/08/2018. How do I achieve this scenario?? Kindly help.

Maha Waqar
  • 585
  • 1
  • 10
  • 24
  • What is the type of `ArrivalDate`. Your Calendar's instance is of `now` since that is how you instantiated. It is better to use the `LocalDate` or `LocalDateTime` instead of `Date` and `Calendar` – gtgaxiola Aug 01 '18 at 17:43
  • Search Stack Overflow thoroughly before posting. The issues of [parsing strings into date-time values](https://stackoverflow.com/search?q=java+date+parse+parsing), [incrementing a date](https://stackoverflow.com/search?q=java+date+add), [using modern *java.time* classes with JDateChooser](https://duckduckgo.com/?q=site%3Astackoverflow.com+%2BJDateChooser+%2Bjava.time&t=osx&ia=web), and [avoiding the terrible `Calendar` & `SimpleDateFormat` classes](https://stackoverflow.com/search?q=java+java.time+avoid+legacy) have all been handled many times already on Stack Overflow. – Basil Bourque Aug 01 '18 at 21:06

1 Answers1

0

Using LocalDate and DateTimeFormmatter instead of Date and Calendar can be done like this:

public static void main(String[] args) {
    String arrivalDate = "12/08/2018";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate arrival = LocalDate.parse(arrivalDate, formatter);
    LocalDate futureDay = arrival.plusDays(2);
    String nextDate = futureDay.format(formatter);
    System.out.println("Arrival: " + arrivalDate);
    System.out.println("Next Day: " + nextDate);
}

The output:

Arrival: 12/08/2018
Next Day: 14/08/2018
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • I knew this method too. Issue over here is that I want user to choose date from JDateChooser and date choosen from JDateChooser can not be parsed into LocalDate. Now what should I do? Is there is any other alternative of JDateChooser as I don't want user to enter date string, I just want him/her to select a date. – Maha Waqar Aug 01 '18 at 18:59
  • Just convert Date to LocalDate [SO Post about it](https://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate) – gtgaxiola Aug 01 '18 at 19:01
  • Thank you so much for your code.. This worked for me. – Maha Waqar Aug 01 '18 at 19:31