-2

I am using jdatechooser in Netbeans and I have two dates as output date1 = 3/25/17 date2 = 3/30/17

I want a way of subtracting this two dates to get differece in days e.g: 3 Days and put my results into a jtexftield

c0der
  • 18,467
  • 6
  • 33
  • 65
Beroo
  • 29
  • 8
  • 3
    Possible duplicate of [How do you subtract Dates in Java?](http://stackoverflow.com/questions/3526485/how-do-you-subtract-dates-in-java) – csirmazbendeguz Mar 27 '17 at 10:19
  • 1
    Welcome to SO. Please make an attempt and post here a [MCVE] version of your attempt to solve it – c0der Mar 27 '17 at 10:50
  • I have used `joda-time` now I am getting error : `Invalid format: "3/27/17" is malformed at "/27/17"` – Beroo Mar 27 '17 at 12:45

1 Answers1

0

You can parse you date to LocaDate and then calculate difference using Period:

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("M/d/yy");
LocalDate start = LocalDate.parse("3/25/17",dateTimeFormatter);
LocalDate  end = LocalDate.parse("3/30/17",dateTimeFormatter);
System.out.println(Period.between(start,end).getDays());

For pre java-8 code please see this question

Community
  • 1
  • 1
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53