-1

I have used Jodha Library and tried to get the difference between two dates in seconds. But it is only accurate up to the date. Not to the seconds.

 public static int getDateDifference(DateTime dateCreatedPa)
    {
        LocalDate dateCreated = new LocalDate (dateCreatedPa);
        LocalDate now = new LocalDate();

        Seconds secondsBetween = Seconds.secondsBetween(dateCreated, now);

        return secondsBetween.getSeconds();
    }
       ///code calling the above method

        DateTime dateCreated=new DateTime(drivingLicense.getDateCreated());
        int dateDiff=Common.getDateDifference(dateCreated);            
        request.setAttribute("dateDiff", dateDiff);

        System.out.println("Timestamp: "+dateDiff);

This shows the date difference. But if I give different times on the same date for comparison, it returns 0.

TheShadow123
  • 185
  • 2
  • 14

1 Answers1

1

LocalDate is just that, date only, no time information. Use LocalDateTime instead

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.Seconds;

public class Test {

    public static void main(String[] args) {

        DateTime today = DateTime.now();
        today = today.minusDays(5);
        int dateDiff = getDateDifference(today);
        System.out.println("Timestamp: " + dateDiff);
    }

    public static int getDateDifference(DateTime dateCreatedPa) {
        LocalDateTime dateCreated = new LocalDateTime(dateCreatedPa);
        LocalDateTime now = new LocalDateTime();

        System.out.println(dateCreated);
        System.out.println(now);

        Seconds secondsBetween = Seconds.secondsBetween(dateCreated, now);

        return secondsBetween.getSeconds();
    }

}

Outputs...

2015-02-27T15:20:56.524
2015-03-04T15:20:56.628
Timestamp: 432000
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366