我想从startDate和endDate中找出它多少天。从我的json响应中,我可以在String中找回它们:
String startDate = "Fri, 10 Jan 2015 23:10:04 +0000";
String endDate = "Mon, 26 Jan 2015 11:26:04 +0000";
我如何使用joda计算并让我回到正确的天数(因此:numOfDays = endDate - startDate)??
public int getNumberOfDays(String startDate, String endDate) {
String pattern = "EEE, dd MMM yyyy HH:mm:ss +xxxx";
.....
// quick test on startDate
DateTime dateTime = DateTime.parse(startDate, DateTimeFormat.forPattern(pattern));
System.out.println("The day should be 10 but it yields 9 --> " + dateTime.getDayOfMonth());
}
答案 0 :(得分:1)
使用天类Joda资料库
Days days=Days.daysBetween(startDate.toLocalDate(), endDate.toLocalDate()).getDays();
int noOfDays=days.getDays();
作为替代方案,我们也可以使用 Java 8 日期和时间API
public void days_between_two_dates_in_java_with_java8 () {
LocalDate startDate = LocalDate.now().minusDays(1);
LocalDate endDate = LocalDate.now();
long days = Period.between(startDate, endDate).getDays();
// or
long days2 = ChronoUnit.DAYS.between(startDate, endDate);
}