This question already has answers here:
Android calculate days between two dates
(11个答案)
How to get number of days between two calendar instance?
(11个答案)
2天前关闭。
我创建一个预订酒店的应用程序,它具有3 textview和2按钮来选择日期。如果单击按钮,它将显示一个日期,所选日期将显示为2个文本视图。如何在第三文本视图中显示日期之间的日期?
这是我的代码(第三个文本视图未显示为日期之间的日期)
同样,在第二个日期选择器的
从两个
参考:How to get number of days between two calendar instance?
(11个答案)
How to get number of days between two calendar instance?
(11个答案)
2天前关闭。
我创建一个预订酒店的应用程序,它具有3 textview和2按钮来选择日期。如果单击按钮,它将显示一个日期,所选日期将显示为2个文本视图。如何在第三文本视图中显示日期之间的日期?
这是我的代码(第三个文本视图未显示为日期之间的日期)
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
tvDateResult = (TextView) findViewById(R.id.tv_dateresult);
res = findViewById(R.id.tv_dateresult1);
ress = findViewById(R.id.tv_dateresult2);
btDatePicker = (Button) findViewById(R.id.bt_datepicker);
date = findViewById(R.id.bt_datepicker1);
get = findViewById(R.id.getprice);
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDateDialog2();
}
});
btDatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDateDialog();
}
});
}
private void showDateDialog2() {
Calendar newCalendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
res.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
if(tvDateResult !=null || res !=null) {
try {
String a = tvDateResult.getText().toString();
String b = res.getText().toString();
Date aa = dateFormatter.parse(a);
Date bb = dateFormatter.parse(b);
long diff = bb.getTime() - aa.getTime() / 24 * 60 * 60 * 1000;
ress.setText((int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
private void showDateDialog(){
Calendar newCalendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
tvDateResult.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
最佳答案
声明2个全局变量:Calendar date1 = null, date2 = null;
在两个日期选择器的onDateSet
方法中,将所选日期保存在全局变量中。
在第一个日期选择器的onDateSet
中,
Calendar newDate = Calendar.getInstance();
date1 = newDate
updateTextView()
同样,在第二个日期选择器的
onDateSet
中,Calendar newDate = Calendar.getInstance();
date1 = newDate
updateTextView()
从两个
onDateSet
方法中调用一个方法。private void updateTextView() {
if(date1 != null && date2 != null) {
tvDateResult .setText(dateFormatter.format(daysBetween(date1,date2)));
}
}
public static long daysBetween(Calendar startDate, Calendar endDate) {
// Make sure we don't change the parameter passed
Calendar newStart = Calendar.getInstance();
newStart.setTimeInMillis(startDate.getTimeInMillis());
newStart.set(Calendar.HOUR_OF_DAY, 0);
newStart.set(Calendar.MINUTE, 0);
newStart.set(Calendar.SECOND, 0);
newStart.set(Calendar.MILLISECOND, 0);
Calendar newEnd = Calendar.getInstance();
newEnd.setTimeInMillis(endDate.getTimeInMillis());
newEnd.set(Calendar.HOUR_OF_DAY, 0);
newEnd.set(Calendar.MINUTE, 0);
newEnd.set(Calendar.SECOND, 0);
newEnd.set(Calendar.MILLISECOND, 0);
long end = newEnd.getTimeInMillis();
long start = newStart.getTimeInMillis();
return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start));
}
参考:How to get number of days between two calendar instance?
08-17 14:55