URL87
URL87

Reputation: 11012

Get remaining days between now to DateMidnight class

I have a due date as DateMidnight type - DateMidnight return_due_date and I want to compute the days left from now until this date .

How can I compute that ?

DateMidnight documentation

Upvotes: 2

Views: 662

Answers (4)

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9745

here comes simple & nice library for such purposes
https://code.google.com/p/stringtotime/

Upvotes: 0

user1555863
user1555863

Reputation: 2607

This is how it's done:

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.Days;


public class example {

/**
 * How many days until a certain date.
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    DateMidnight someDate = new DateMidnight(2013,6,26);
    System.out.println("An example due date: "+someDate);
    DateTime timeNow = new DateTime();
    System.out.println("The time right now: "+timeNow);
    int daysToDeadLine = Days.daysBetween(timeNow, someDate).getDays();
    System.out.println("Days until deadline: "+daysToDeadLine);

}}

May I also suggest a little more effort on your behalf next time? :)

Upvotes: 0

Brent Worden
Brent Worden

Reputation: 10974

Use the org.joda.time.Days class. It provides some daysBetween methods to compute exactly what you need.

DateMidnight midnight = return_due_date; // your DateMidnight instance
DateTime now = new DateTime();

int daysBetween = Days.daysBetween(now, midnight).getDays();

Upvotes: 1

Tala
Tala

Reputation: 8928

int days = org.joda.time.Days.daysBetween(DateMidnight.now(), yourDueDate).getDays(); 

Upvotes: 4

Related Questions