当我按下右侧箭头日期时,textView正在递增,我需要在datePicker对话框上设置日期。当我按下刷新然后它设置为当前日期但不是当我按向左箭头和向右箭头。我该怎么做才能实现这一目标?
iv_refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count = 0;
getDate(count);
setDateTimeField();
}
});
rightarrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDate(1);
}
});
leftarraow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDate(-1);
}
});
tv_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
icount=0;
DOBPickerDialog.show();
}
});
public long printDifference(Date startDate, Date endDate){
//milliseconds
long different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
System.out.printf(
"%d days, %d hours, %d minutes, %d seconds%n",
elapsedDays,
elapsedHours, elapsedMinutes, elapsedSeconds);
return elapsedDays;
}
private void getDate(int i) {
count = count + i;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, count);
currenttoday = dateFormat.format((calendar.getTime()));
// currenttoday = currenttoday.replace("-","/");
tv_date.setText(currenttoday);
// Toast.makeText(LandingActivity.this, currenttoday, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction("DateActivity");
intent.putExtra("date", currenttoday);
sendBroadcast(intent);
}
/**
* setDatetimefield method is used to set the selected date
* from the datepicker to the text view
*/
private void setDateTimeField() {
java.util.Calendar newCalendar = java.util.Calendar.getInstance();
DOBPickerDialog = new DatePickerDialog(LandingActivity.this,R.style.pickertheme, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
String selectDate = dateFormatter.format(newDate.getTime());
tv_date.setText(selectDate);
try {
Date date1 = dateFormatter.parse(currenttoday);
String newDat = dateFormat.format(newDate.getTime());
Date date2 = dateFormatter.parse(newDat);
long diffDays = printDifference(date1, date2);
icount = (int) diffDays;
getDate(icount);
} catch (ParseException e) {
e.printStackTrace();
}
}
}, newCalendar.get(java.util.Calendar.YEAR), newCalendar.get(java.util.Calendar.MONTH), newCalendar.get(java.util.Calendar.DAY_OF_MONTH));
}
答案 0 :(得分:0)
DatePickerDialog(上下文上下文, int themeResId, DatePickerDialog.OnDateSetListener侦听器, int年, int monthOfYear, int dayOfMonth)
在这里,您需要传递最初要在选择器中显示的年,月和日的第一个月信息。
java.util.Calendar newCalendar = java.util.Calendar.getInstance();
在这里,您将获得一个新的日历实例,默认设置为当前日期。并致电newCalendar.get(java.util.Calendar.YEAR), newCalendar.get(java.util.Calendar.MONTH), newCalendar.get(java.util.Calendar.DAY_OF_MONTH)
你告诉日期选择器将它设置为当前日期。所以在这里你需要传递textview中的日期信息