Reputation: 11012
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 ?
Upvotes: 2
Views: 662
Reputation: 9745
here comes simple & nice library for such purposes
https://code.google.com/p/stringtotime/
Upvotes: 0
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
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
Reputation: 8928
int days = org.joda.time.Days.daysBetween(DateMidnight.now(), yourDueDate).getDays();
Upvotes: 4