2

The variable dateSubtract comes out to 16 but I want to find the total number of days in between the 2 days, which should be 165. How can I do this WITHOUT JODA TIME?

String date = "06/17/2014";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate d1 = LocalDate.parse("01/01/2014", formatter);
LocalDate d2 = LocalDate.parse(date, formatter);
int dateSubtract = Period.between(d1, d2).getDays();
assylias
  • 321,522
  • 82
  • 660
  • 783
user2007843
  • 609
  • 1
  • 12
  • 30
  • Please check whether this link can help you ?? [Link]http://stackoverflow.com/questions/13922496/right-period-between-two-java-dates – Rakesh Burbure Jun 18 '14 at 15:23
  • 1
    @RakeshBurbure well, that link either implies using Joda Time or pre-Java-8 date calculations. – Thomas Jun 18 '14 at 15:24

3 Answers3

9

Period is a combination of day, month, year. So in your case, the period is 5 months and 16 days. It is explained in the javadoc although not necessarily very clear if you read it casually.

The days unit is not automatically normalized with the months and years unit. This means that a period of "45 days" is different to a period of "1 month and 15 days" and getDays() will return 45 and 15 respectively.

To get the total number of days between two dates, you can use:

//including d1, excluding d2:
ChronoUnit.DAYS.between(d1, d2);
//or, to exclude d1 AND d2, one of these:
ChronoUnit.DAYS.between(d1.plusDays(1), d2);
ChronoUnit.DAYS.between(d1, d2) - 1;
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I'm not using Java 1.8 so right now `ChronoUnit` isnt supported. hmmm – user2007843 Jun 18 '14 at 15:32
  • @user2007843 AFAIK `LocalDate`, `Period` etc. are only provided by Java 8, so either you're using Java 8 or a different library, which would make it harder to help, without knowing which one. – Thomas Jun 18 '14 at 15:38
  • I apparently have Java 8 installed and I can import `java.time.LocalDate` and `java.time.Period` but ChronoUnit doesn't work – user2007843 Jun 18 '14 at 15:42
  • @user2007843 `import java.time.temporal.ChronoUnit;`. – assylias Jun 18 '14 at 15:46
  • Okay yeah I just did that, I guess it didn't automatically add it when I typed ChronoUnit. Thanks – user2007843 Jun 18 '14 at 15:47
0

Without JODA time:

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date dateStart = null;
Date dateEnd = null;

try {
    dateStart = format.parse("01/01/2014");
    dateEnd = format.parse("06/17/2014");

    long diffTime = dateEnd.getTime() - dateStart.getTime();

    long diffDays = diffTime / (24 * 60 * 60 * 1000);

} catch (Exception e) {
    e.printStackTrace();
}
user1071777
  • 1,307
  • 1
  • 15
  • 23
  • That assumes a well behaved time zone. – assylias Jun 18 '14 at 15:29
  • I've also done this and I get 166 days, JodaTime is the only way I have done it to get 165 – user2007843 Jun 18 '14 at 15:30
  • @user2007843 shouldn't it actually be 167 days between 01/01/2014 and 06/17/2014? – Thomas Jun 18 '14 at 15:32
  • @Thomas well thats counting the start and end date, which I don't want to do – user2007843 Jun 18 '14 at 15:33
  • @user2007843 I didn't count the end date either, which would be the 168th day (167 _full_ days, i.e. 01/01/14 00:00:00 to 06/17/14 00:00:00) so if you don't count January 1st, it should still be 166 days, shouldn't it? – Thomas Jun 18 '14 at 15:35
  • @Thomas So on a smaller scale are you saying that there are 2 days between Jan 1st and Jan 4th? Because that's what I am looking for – user2007843 Jun 18 '14 at 15:38
  • @user2007843 that depends on how you count that. Including the boundaries it would be 3 _full_ days and a fraction of the 4th day (even if it would be 0.9999998 a day), if you don't count the start and end days (i.e. you only count Jan 2nd and 3rd), you'd have 2 days. – Thomas Jun 18 '14 at 15:41
0

Period models a quantity or amount of time in terms of years, months and days, such as 2 years, 3 months and 4 days.

To calculate days between two date use ChronoUnit.DAYS.between

long days = ChronoUnit.DAYS.between(LocalDate.of(2020,4,1), LocalDate.now());
Eklavya
  • 17,618
  • 4
  • 28
  • 57