1

I have a String representing a date with format yyyy/mm/dd and want to calculate the number of days between that day and today. How is this done?

So It may be something like

String aDate = "1934/05/24"
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
William Roberts
  • 319
  • 2
  • 5
  • 21
  • I've look around joda Time, but can't find seem to find a method that calculates the day between two strings, just ReadableInstants which cannot be instantiated. – William Roberts Mar 19 '14 at 15:37
  • This will answer your question : https://stackoverflow.com/questions/7103064/java-calculate-the-number-of-days-between-two-dates – Clad Clad Mar 19 '14 at 15:38
  • You are asking two different questions: (a) How to [parse strings into date-time](http://stackoverflow.com/q/20098644/642706) objects, and (b) How to [calculate a span of time](http://stackoverflow.com/q/3526485/642706). Both have been answered many times here on StackOverflow. You definitely should be using [Joda-Time](http://www.joda.org/joda-time/) or the new java.time package in Java 8, as the old bundled java.util.Date & .Calendar classes are notoriously troublesome. Search for "joda parse" and "joda span". And search for Joda-Time's 3 span-of-time classes: Period, Duration, and Interval. – Basil Bourque Mar 19 '14 at 16:56
  • In java.time code: `ChronoUnit.DAYS.between( LocalDate.parse( "1934/05/24".replace( "/" , "-" ) ) , LocalDate.now() )` – Basil Bourque Oct 26 '17 at 23:42

3 Answers3

3

Way to do this -

  1. Parse the date string to Date object using SimpleDateFormat.
  2. Get the date difference between todays date and parsed date.

    long diff = d2.getTime() - d1.getTime();//in milliseconds

  3. Now convert it to days, hours, minutes and seconds.

    • 1000 milliseconds = 1 second
    • 60 seconds = 1 minute
    • 60 minutes = 1 hour
    • 24 hours = 1 day

Other way using joda date time

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/mm/dd");
DateTime dt1 = formatter.parseDateTime(aDate);

And

Duration duration = new Duration(dt1 , new DateTime());
System.out.println("Days "+duration.getStandardDays()+" Hours "+duration.getStandardHours()+" Minutes "+duration.getStandardMinutes());
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

The way to get the number of days between two dates with Joda-Time is to use Days.daysBetween but the Days class isn't part of the Java 8 java.time package.

Instead, in Java 8, you can do this:

public static long daysBetween(ChronoLocalDate first, ChronoLocalDate second) {
    return Math.abs(first.until(second, ChronoUnit.DAYS));
}

ChronoLocalDate is an interface implemented by LocalDate, so you can pass two LocalDates to this method and get the difference between them in days. (However, there are some caveats (see Using LocalDate instead) with using ChronoLocalDate in place of LocalDate in APIs. It might be better to just declare the parameters as LocalDates.)

David Conrad
  • 15,432
  • 2
  • 42
  • 54
0

Use SimpleDateFormat with the proper mask and convert it to Date and then use a Calendar object to calculate the days between (like in this example: https://stackoverflow.com/a/8912994/79230).

Community
  • 1
  • 1
Averroes
  • 4,168
  • 6
  • 50
  • 63