我编写了以下代码来查找两个日期之间的日期

startDateValue = new Date(startDate);
endDateValue = new Date(endDate);
long diff = endDateValue.getTime() - startDateValue.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = (hours / 24) + 1;
Log.d("days","" + days);

当开始日期和结束日期分别是2/3/2017和3/3/2017时,显示的天数是29.尽管它们是同一天,但显示的是1.(请假的天数。 如果请假一天,他必须选择相同的开始日期和结束日期。因此,在这种情况下,他请了两天假。

我究竟做错了什么?

感谢您的时间。

注意:请不要使用日期构造函数。 检查下面接受的答案。 使用simpledateformat或Joda时间。 不推荐使用日期构造函数。

startDate和endDate到底是什么? (目前我们甚至都不知道它们的类型。)如果您可以改用Joda Time,那会更好,顺便说一句。

@JonSkeet startDate =(字符串)2/3/2017 endDate =(字符串)3/3/2017

那么您使用不推荐使用的Date(String)构造函数吗?首先,我要停止这样做。 ID建议使用具有特定格式的SimpleDateFormat并使用UTC时区。 (很可能是由于DST转换,您得到了错误的结果-在不知道您所在的时区的情况下很难知道。)

@JonSkeet好,我已经实现了Sachin答案,它使用了简单的日期格式。从现在开始将不再使用Date构造函数。

很高兴听到。该构造函数已在20年前被弃用:)

@JonSkeet :)谢谢

仅供参考,麻烦的旧日期时间类(例如java.util.Date,java.util.Calendar和java.text.SimpleDateFormat)现在已被遗留,由内置在Java 8和Java 9中的java.time类取代。请参见Oracle教程。参见Anton Balaniuc的现代答案。

按照下面的链接它将对您有用。简单的方法stackoverflow.com/a/51244555/5697474

您的用于生成日期对象的代码:

Date date = new Date("2/3/2017"); //deprecated

您得到28天作为答案,因为根据Date(String)构造函数,它认为day = 3,month = 2和year = 2017

您可以将String转换为Date,如下所示:

String dateStr ="2/3/2017";

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

Date date = sdf.parse(dateStr);

使用上面的模板制作Date对象。然后使用下面的代码计算两个日期之间的天数。希望这清楚的事情。

它可以完成如下操作:

long diff = endDateValue.getTime() - startDateValue.getTime();

System.out.println ("Days:" + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));

请检查链接

如果使用Joda Time,它将更加简单:

int days = Days.daysBetween(date1, date2).getDays();

请检查JodaTime

如何在Java Project中使用JodaTime

它仍显示2017年2月3日,2017年3月3日的28天。请使用joda时间并通知您

您能告诉我们startDate和endDate变量的值是多少。

startDate =(字符串)2/3/2017 endDate =(字符串)3/3/2017

不建议使用此方法,最好使用SimpleDateFormat。您是否只想继续执行此代码?您可以在此处检查docs.oracle.com/javase/7/docs/api/java/util/Date.html

好的,那么我将尝试使用SimpleDateFormat,但是稍后。现在我想尽快纠正这个问题

pf字符串日期的格式是什么。是" MM / dd / yyyy"吗?

它是dd / MM / yyyy。所以2/3/2017是天= 2,月= 3和年= 2017

由于编译器正在考虑day = 3,month = 2 and year = 2017,因此您有28天的时间

是的,我只是看到了。如果我将月份的位置与日期交换,反之亦然,那么结果是正确的。不知道为什么格式必须是MM / dd / yyyy而不是dd / MM / yyyy。并通知你

请检查有关Date对象的Oracle文档。链接已在上部注释中给出。您将了解为什么格式是这样的。

谢谢您的时间。日期格式对我来说是错误的。我赞成并接受了您的回答

谢谢@ debo.stackoverflow。乐意效劳。

public static int getDaysDifference(Date fromDate,Date toDate)
{
if(fromDate==null||toDate==null)
return 0;
return (int)( (toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
}

您使用什么日期格式?是d/M/yyyy还是M/d/yyyy?

d =日,M =月,yyyy =年

(请参阅:https://developer.android.com/reference/java/text/SimpleDateFormat.html)

然后代码:

public static final String DATE_FORMAT ="d/M/yyyy";  //or use"M/d/yyyy"
public static long getDaysBetweenDates(String start, String end) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
Date startDate, endDate;
long numberOfDays = 0;
try {
startDate = dateFormat.parse(start);
endDate = dateFormat.parse(end);
numberOfDays = getUnitBetweenDates(startDate, endDate, TimeUnit.DAYS);
} catch (ParseException e) {
e.printStackTrace();
}
return numberOfDays;
}
对于getUnitBetweenDates方法:
private static long getUnitBetweenDates(Date startDate, Date endDate, TimeUnit unit) {
long timeDiff = endDate.getTime() - startDate.getTime();
return unit.convert(timeDiff, TimeUnit.MILLISECONDS);
}
Android是否完全支持java-8?如果是,则可以简单地使用ChronoUnit类
LocalDate start = LocalDate.of(2017,2,3);
LocalDate end = LocalDate.of(2017,3,3);
System.out.println(ChronoUnit.DAYS.between(start, end)); // 28
或使用格式化程序的同一件事
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/yyyy");
LocalDate start = LocalDate.parse("2/3/2017",formatter);
LocalDate end = LocalDate.parse("3/3/2017",formatter);
System.out.println(ChronoUnit.DAYS.between(start, end)); // 28

在Android上不可用。

仅供参考,麻烦的旧日期时间类(如java.util.Date,java.util.Calendar和java.text.SimpleDateFormat)现在已被遗留,由java.time类取代。在ThreeTen-Backport项目中,许多java.time功能都被反向移植到Java 6和Java 7。在ThreeTenABP项目中进一步适用于早期的Android。请参阅如何使用ThreeTenABP…。

科特林

这是计算从今天到某个日期的天数的示例:

val millionSeconds = yourDate.time - Calendar.getInstance().timeInMillis

leftDays.text = TimeUnit.MILLISECONDS.toDays(millionSeconds).toString() +"days"

如果要计算两天,请更改:

val millionSeconds = yourDate1.time - yourDate2.time

应该管用。

很好的科特琳例子

非常简单,只需使用日历,为两个日期创建两个实例,转换为毫秒,减去并转换为天(四舍五入)...基本上是这样的:

Calendar startDate = Calendar.getInstance();
startDate.set(mStartYear, mStartMonth, mStartDay);
long startDateMillis = startDate.getTimeInMillis();
Calendar endDate = Calendar.getInstance();
endDate.set(mEndYear, mEndMonth, mEndDay);
long endDateMillis = endDate.getTimeInMillis();
long differenceMillis = endDateMillis - startDateMillis;
int daysDifference = (int) (differenceMillis / (1000 * 60 * 60 * 24));

看一下这段代码,这对我很有帮助,希望它将对您有所帮助。

public String get_count_of_days(String Created_date_String, String Expire_date_String) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
Date Created_convertedDate = null, Expire_CovertedDate = null, todayWithZeroTime = null;
try {
Created_convertedDate = dateFormat.parse(Created_date_String);
Expire_CovertedDate = dateFormat.parse(Expire_date_String);
Date today = new Date();
todayWithZeroTime = dateFormat.parse(dateFormat.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
int c_year = 0, c_month = 0, c_day = 0;
if (Created_convertedDate.after(todayWithZeroTime)) {
Calendar c_cal = Calendar.getInstance();
c_cal.setTime(Created_convertedDate);
c_year = c_cal.get(Calendar.YEAR);
c_month = c_cal.get(Calendar.MONTH);
c_day = c_cal.get(Calendar.DAY_OF_MONTH);
} else {
Calendar c_cal = Calendar.getInstance();
c_cal.setTime(todayWithZeroTime);
c_year = c_cal.get(Calendar.YEAR);
c_month = c_cal.get(Calendar.MONTH);
c_day = c_cal.get(Calendar.DAY_OF_MONTH);
}
/*Calendar today_cal = Calendar.getInstance();
int today_year = today_cal.get(Calendar.YEAR);
int today = today_cal.get(Calendar.MONTH);
int today_day = today_cal.get(Calendar.DAY_OF_MONTH);
*/
Calendar e_cal = Calendar.getInstance();
e_cal.setTime(Expire_CovertedDate);
int e_year = e_cal.get(Calendar.YEAR);
int e_month = e_cal.get(Calendar.MONTH);
int e_day = e_cal.get(Calendar.DAY_OF_MONTH);
Calendar date1 = Calendar.getInstance();
Calendar date2 = Calendar.getInstance();
date1.clear();
date1.set(c_year, c_month, c_day);
date2.clear();
date2.set(e_year, e_month, e_day);
long diff = date2.getTimeInMillis() - date1.getTimeInMillis();
float dayCount = (float) diff / (24 * 60 * 60 * 1000);
return ("" + (int) dayCount +" Days");
}

谢谢我尝试了这段代码,它正在工作。我投票了,但我接受了最简单的答案。无论如何,感谢您的时间

如果您想使用接收到的整数,请谨慎操作,例如指示自定义日历实施中的特定日期。例如,我尝试通过从1970年1月1日到选定的日期的日期,从月历视图进入每日视图并显示每日内容,并且每个月的第25-31天将我显示为一天前,因为datesDifferenceInMillis / (24 * 60 * 60 * 1000);可能返回类似17645,95833333333的内容,并将其强制转换为int,您将获得的值降低1。在这种情况下,通过使用NumberFormat类四舍五入接收到的浮点数可以正确地获得天数。这是我的代码:

NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
numberFormat.setRoundingMode(RoundingMode.HALF_UP);
numberFormat.setMaximumFractionDigits(0);
numberFormat.setMinimumFractionDigits(0);
int days = numberFormat.parse(numberFormat.format(value)).intValue();

希望对您有所帮助。

日子并非总是24小时长,因此这并不准确。使用ChronoUnit.DAYS.between( start , stop )更容易。