Number of days between two dates in Joda-Time

Number of days between two dates in Joda-Time

Asked on December 3, 2018 in Java.
Add Comment


  • 5 Answer(s)

    The solution code is below here,

    Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays()
    

    It  yields that “midnight/start of day” sometimes means 1am (daylight savings happen this way in some places), which Days.daysBetween doesn’t handle properly.

    // 5am on the 20th to 1pm on the 21st, October 2013, Brazil
    DateTimeZone BRAZIL = DateTimeZone.forID("America/Sao_Paulo");
    DateTime start = new DateTime(2013, 10, 20, 5, 0, 0, BRAZIL);
    DateTime end = new DateTime(2013, 10, 21, 13, 0, 0, BRAZIL);
    System.out.println(daysBetween(start.withTimeAtStartOfDay(),
    end.withTimeAtStartOfDay()).getDays());
    // prints 0
    System.out.println(daysBetween(start.toLocalDate(),
    end.toLocalDate()).getDays());
    // prints 1
    

    You have to go through the LocalDate sidesteps the whole issue.

    Answered on December 3, 2018.
    Add Comment

    This method surely works,]

    We are going to use the Days class with the withTimeAtStartOfDay..

    Days.daysBetween(start.withTimeAtStartOfDay() , end.withTimeAtStartOfDay() ).getDays()
    

     

    Answered on December 3, 2018.
    Add Comment

    Try to do this, this should works.

    Here we can use the LocalDate

    Days.daysBetween(new LocalDate(start), new LocalDate(end)).getDays()
    
    Answered on December 3, 2018.
    Add Comment

    he following code snippet show you how to get days between two dates. You can use the Days.daysBetween() method and pass the dates you want to calculate. This method return a Days object. To get the number in days you call the getDays() method.

    package org.kodejava.example.joda;
    
    import org.joda.time.DateMidnight;
    import org.joda.time.Days;
    import org.joda.time.LocalDate;
    import org.joda.time.Months;
    
    public class DaysBetweenDemo {
        public static void main(String[] args) {
            // Define the start and end dates. We use the DateMidnight
            // class to make sure that the calculation start from the
            // midnight.
            DateMidnight start = new DateMidnight("2017-01-01");
            DateMidnight end = new DateMidnight("2017-01-16");
    
            // Get days between the start date and end date.
            int days = Days.daysBetween(start, end).getDays();
    
            // Print the result.
            System.out.println("Days between " +
                    start.toString("yyyy-MM-dd") + " and " +
                    end.toString("yyyy-MM-dd") + " = " +
                    days + " day(s)");
    
            // Using LocalDate object.
            LocalDate date1 = LocalDate.parse("2017-01-01");
            LocalDate date2 = LocalDate.now();
            days = Days.daysBetween(date1, date2).getDays();
    
            // Print the result.
            System.out.println("Days between " +
                    date1.toString("yyyy-MM-dd") + " and " +
                    date2.toString("yyyy-MM-dd") + " = " +
                    days + " day(s)");
    
        }
    }
    Answered on January 18, 2019.
    Add Comment

    Let’s start by using the core Java APIs to do the calculation and determine the number of days between the two dates:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Test
    public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix()
      throws ParseException {
     
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
        Date firstDate = sdf.parse("06/24/2017");
        Date secondDate = sdf.parse("06/30/2017");
        long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
        long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
        assertEquals(diff, 6);
    Answered on January 18, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.