1

I have two dates. I am getting dates from database like this:-

Date d = new Date();
Date dTarget = finance.getTargetDate();

at the moment my d is 2017-12-29 and dTarget is 2017-12-31

Now i am trying to calculate the number of days in between them using Joda time api. I expect number of days to be 2 but at the moment I am getting 1, which I think is incorrect.

My timezone is UTC+5:45

     //joda initital datetime
    DateTime intdt = new DateTime(new Date());
    DateTime targetDt = new DateTime(dTarget);

    int noOfDays = Days.daysBetween(intdt,targetDt).getDays();
    System.out.println(noOfDays);

Am I finding the difference between two dates correctly? Why is it not showing 2 for this case?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
sagar limbu
  • 1,202
  • 6
  • 27
  • 52
  • 1
    Try this Days.daysBetween(intdt.toLocalDate(), targetDt.toLocalDate()).getDays() – Vishnu T S Dec 29 '17 at 06:47
  • 1
    You could try this `Days.daysBetween(new LocalDate(start), new LocalDate(end)).getDays();` – N00b Pr0grammer Dec 29 '17 at 06:48
  • thanks after using Localdate it is now showing 2 days. now i think i am getting differences correctly. thanks vts and noob programmer. – sagar limbu Dec 29 '17 at 06:50
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Dec 29 '17 at 08:00
  • 1
    Please search Stack Overflow before posting. You can assume any basic date-time question has already been asked and answered. Tip: `java.time.temporal.ChronoUnit.DAYS.between( LocalDate.parse("2017-12-29" ) , LocalDate.parse("2017-12-31" ) )` is `2`. – Basil Bourque Dec 29 '17 at 08:01

2 Answers2

2

Use this

 Days.daysBetween(intdt.toLocalDate(), targetDt.toLocalDate()).getDays() 

The LocalDate class does not store or represent a time or time-zone. Instead, it is a description of the date.

Vishnu T S
  • 3,476
  • 2
  • 23
  • 39
0
SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
String inputString1 = "23 01 1997";
String inputString2 = "27 04 1997";

try {
    Date date1 = myFormat.parse(inputString1);
    Date date2 = myFormat.parse(inputString2);
    long diff = date2.getTime() - date1.getTime();
    System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
    e.printStackTrace();
}
Dilip
  • 2,622
  • 1
  • 20
  • 27
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. Even less when they are already using something better, in this case Joda-Time. – Ole V.V. Dec 29 '17 at 07:44
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Dec 29 '17 at 08:02