2

I was doing a simple calculation to get the difference between two dates. If I was using a Date class I can do like this:

Date d1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();

    /** Today's date */
    Date today = new Date();

    // Get msec from each, and subtract.
    long diff = today.getTime() - d1.getTime();

    System.out.println("The 21st century (up to " + today + ") is "
        + (diff / (1000 * 60 * 60 * 24)) + " days old.");
  }

But I couldn't find a method like getTime() in Local date. Is there any way so I can easily get what I am trying to achieve?

I even tried to change my LocalDate object to a temporary date object like this:

 LocalDate date=new LocalDate();
    Date d=date.toDate();

but the method toDate() isnt working . i.e it says it is not recognized method.(so compile time error) but from what I can see it is in the Documentation

Thank you for your time and of course happy Thanksgiving.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
WowBow
  • 7,137
  • 17
  • 65
  • 103
  • I tried that one and the method is protected – WowBow Nov 22 '11 at 21:28
  • `LocalDate` is meant to hold only a date - year, month, day. If you need a local date and time, use `LocalDateTime` instead. – Jesper Nov 22 '11 at 21:29
  • Your calculation is wrong. You are assuming that a day is always 24 hours. Because of DST and many other anomalies this is not so. – Ole V.V. Nov 15 '22 at 11:58
  • For most purposes you don’t need milliseconds since the epoch, and as I said, for counting days it’s wrong to use them. In case someone does need them, the answer to the question in your title is `date.toDateTimeAtStartOfDay(DateTimeZone.getDefault()).getMillis()` (where `date` is your Joda-Time `LocalDate`). – Ole V.V. Nov 15 '22 at 19:21

5 Answers5

2

Days.daysBetween() is the answer.

LocalDate now = new LocalDate();
LocalDate past = now.minusDays(300);
int days = Days.daysBetween(past,now).getDays();

Never convert a LocalDate to a Java Date (two completey different beasts) if you are just dealing with dates. A Jodatime Localdate is a true "Calendar date", i.e. , a tuple of {day,month,year} (together with a Gregorian calendar specification), and has nothing to do with "physical time", with seconds, hours, etc. If you need to do dates arithmetic, stick with Localdate and you'll never need to worry about stupid bugs (timezones, DST, etc) which could arise if you dates arithmetic using java Dates.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
1

Try something like this:

      LocalDate date =  new LocalDate();

       Date utilDate = date.toDateTimeAtStartOfDay( timeZone ).toDate( );

or refer to this post

How to convert Joda LocalDate to java.util.Date?

Community
  • 1
  • 1
amit modi
  • 1,098
  • 1
  • 11
  • 26
  • Milliseconds since the epoch were asked about, and the outdated `Date` class is a dumb detour to make to get them. Use just `date.toDateTimeAtStartOfDay(DateTimeZone.getDefault()).getMillis()` (supply a different time zone as needed). – Ole V.V. Nov 16 '22 at 10:05
0

Have not found any equivalents for LocalDate as they are not exact. But there are several equivalents for LocalDateTime:

LocalDateTime localDateTime = LocalDateTime.now();
long longValue = ZonedDateTime.of(localDateTime, ZoneId.systemDefault()).toInstant().toEpochMilli();

or

long longValue = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

or

long longValue = localDateTime.toInstant(OffsetDateTime.now().getOffset()).toEpochMilli();

or

long longValue = Timestamp.valueOf(localDateTime).getTime();
Andriy
  • 1,981
  • 1
  • 12
  • 9
  • This is the java.time answer (not Joda-Time). I'd take one of the first two. The third is more complicated than needed and is not always correct. And we should avoid using the old `Timestamp` class as in the fourth snippet. – Ole V.V. Nov 18 '22 at 02:00
0

I tested this sample code to find out the difference in days, you can find the difference as per your needs.

Please see http://joda-time.sourceforge.net/key_period.html

    LocalDate currentDate = new LocalDate();

    LocalDate previousDate = currentDate.minusDays(1);
    System.out.println(currentDate);
    System.out.println(previousDate);

    Period periodDifference = new Period(currentDate, previousDate, PeriodType.days());
    System.out.println(periodDifference);
r0ast3d
  • 2,639
  • 1
  • 14
  • 18
0
private long diff(Calendar c1, Calendar c2) { 
  long d1 = c1.getTimeInMillis();
  long d2 = c2.getTimeInMillis();
  return ((d2 - d1) / (60*60*24*1000));
}
awm
  • 2,723
  • 2
  • 18
  • 26
  • Date doesn't have that functionality -- easily. Converting it to another format for this purpose is your best bet. Jesper has the other good answer. – awm Nov 22 '11 at 21:38