我有一个截止日期作为DateMidnight
类型 -DateMidnight return_due_date
我想计算从现在到这个日期的剩余天数。
我该如何计算呢?
int days = org.joda.time.Days.daysBetween(DateMidnight.now(), yourDueDate).getDays();
使用org.joda.time.Days类。它提供了一些daysBetween
方法来准确计算您需要的内容。
DateMidnight midnight = return_due_date; // your DateMidnight instance
DateTime now = new DateTime();
int daysBetween = Days.daysBetween(now, midnight).getDays();
这是如何完成的:
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);
}}
下次我还可以代表您多做一些努力吗?:)
这里有用于此类目的的简单而漂亮的库
https://code.google.com/p/stringtotime/