我正在尝试获取天数之间的差额。使用日期选择器对话框显示当前日期
datepicker = findViewById(R.id.date_picker_date);
textView=findViewById(R.id.textView);
datepicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar cldr = Calendar.getInstance();
int day = cldr.get(Calendar.DAY_OF_MONTH);
int month = cldr.get(Calendar.MONTH);
int year = cldr.get(Calendar.YEAR);
String date = day + "/" + (month+1) + "/" + year;
TextView textView=findViewById(R.id.textView);
textView.setText(date);
datepicker.setInputType(InputType.TYPE_NULL);
datepicker.setShowSoftInputOnFocus(false);
// date picker dialog
picker = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
datepicker.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, year, month, day);
picker.show();
没有得到选择日期和当前日期之间的差异
答案 0 :(得分:0)
我使用了下面的代码,它对我来说很好用。同样适用于较低版本。试试这个
public static DateBean getDateDifferenceInDDMMYYYY(Date from, Date to)
{
Calendar fromDate=Calendar.getInstance();
Calendar toDate=Calendar.getInstance();
fromDate.setTime(from);
toDate.setTime(to);
int increment = 0;
int year,month,day;
System.out.println(fromDate.getActualMaximum(Calendar.DAY_OF_MONTH));
if (fromDate.get(Calendar.DAY_OF_MONTH) > toDate.get(Calendar.DAY_OF_MONTH))
{
increment =fromDate.getActualMaximum(Calendar.DAY_OF_MONTH);
}
System.out.println("increment"+increment);
// DAY CALCULATION
if (increment != 0) {
day = (toDate.get(Calendar.DAY_OF_MONTH) + increment) - fromDate.get(Calendar.DAY_OF_MONTH);
increment = 1;
} else {
day = toDate.get(Calendar.DAY_OF_MONTH) - fromDate.get(Calendar.DAY_OF_MONTH);
}
// MONTH CALCULATION
if ((fromDate.get(Calendar.MONTH) + increment) > toDate.get(Calendar.MONTH)) {
month = (toDate.get(Calendar.MONTH) + 12) - (fromDate.get(Calendar.MONTH) + increment);
increment = 1;
} else {
month = (toDate.get(Calendar.MONTH)) - (fromDate.get(Calendar.MONTH) + increment);
increment = 0;
}
// YEAR CALCULATION
year = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) + increment);
DateBean dateDifference = new DateBean(""+year, ""+month, ""+day);
System.out.println(year+"\tYears\t\t"+month+"\tMonths\t\t"+day+"Days");
return dateDifference;
}
和您的DateBean如下
public class DateBean
{
String year, months, days;
public DateBean(String year, String months, String days)
{
this.year = year;
this.months = months;
this.days = days;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getMonths() {
return months;
}
public void setMonths(String months) {
this.months = months;
}
public String getDays() {
return days;
}
public void setDays(String days) {
this.days = days;
}
}
答案 1 :(得分:0)
使用此方法,以“ MM / dd / yyyy”格式传递日期,您将获得以天为单位的两个日期之间的差额。
public static String daysDiff(String cDate, String rDate){
String dayDifference;
try {
//Dates to compare
String CurrentDate = cDate;
String FinalDate = rDate;
Date date1;
Date date2;
SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");
//Setting dates
date1 = dates.parse(CurrentDate);
date2 = dates.parse(FinalDate);
//Comparing dates
long difference = Math.abs(date1.getTime() - date2.getTime());
long differenceDates = difference / (24 * 60 * 60 * 1000);
//Convert long to String
dayDifference = Long.toString(differenceDates);
// Log.e("hit", "HERE: " + dayDifference);
return dayDifference;
} catch (Exception exception) {
// Log.e("DIDN'T WORK", "exception " + exception);
return "";
}
}