【发布时间】:2018-01-26 09:47:08
【问题描述】:
这是我的问题,每当我在 Date Picker 中选择一个日期时,我的系统都会生成一个特定的日期。这是带有警报对话框的日期选择器的图片:
日期选择器:
具体日期应该是+5天根据你当前的日期,例如。我将在 2018 年 1 月 27 日借一本书。我应该在 2018 年 2 月 1 日之前或等于该日期之前归还它。但我的问题是每当我在 Date Picker 上选择 2018 年 1 月 27 日时。日期搞砸了。我该怎么办?
这是我的程序的代码:
public class Borrow extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseBorrow;
private Button btnBorrow;
public TextView tvScheduleDate,tvSchedule,tvReturnDate;
private int day,month,year,hour,minute;
private int dayFinal,monthFinal,yearFinal,returnDay;
// private Date date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_borrow);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() == null) {
finish();
startActivity(new Intent(this, Login.class));
}
Intent intent = getIntent();
String id = intent.getStringExtra(SearchFragment.BOOK_ID);
final String bookTitle = intent.getStringExtra(SearchFragment.BOOK_TITLE);
btnBorrow=(Button)findViewById(R.id.borrowBtn);
databaseBorrow= FirebaseDatabase.getInstance().getReference("Borrow").child(id);
btnBorrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button();
}
});
}
private void Button(){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.borrow_dialog,(ViewGroup) findViewById(R.id.linearLayout));
tvScheduleDate = (TextView) view.findViewById(R.id.tvScheduleDate);
tvReturnDate = (TextView) view.findViewById(R.id.tvReturnDate);
tvSchedule = (TextView)view.findViewById(R.id.tvSchedule);
tvSchedule.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(Borrow.this , Borrow.this , year,month,day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.add(Calendar.DAY_OF_WEEK,6); //for max date
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
datePickerDialog.show();
}
});
dialogBuilder.setView(view);
dialogBuilder.show();
/*
String ids = databaseBorrow.push().getKey();
String buttonText = btnBorrow.getText().toString();
BorrowBook borrow = new BorrowBook(ids,buttonText,bookTitle,yearFinal,dayFinal,monthFinal);
databaseBorrow.child(ids).setValue(borrow);
Toast.makeText(this,"Borrow Successfully",Toast.LENGTH_SHORT).show();*/
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
yearFinal = i;
monthFinal = i1+1;
dayFinal = i2;
tvScheduleDate.setText(dayFinal+"-"+monthFinal+"-"+yearFinal);
// I think I used a wrong algorithm for this one , any suggestions so I
can solve this?
if(dayFinal>=27) {
dayFinal=((i2+5)*0+1);
monthFinal=i1+2;
tvReturnDate.setText(dayFinal + "-" + monthFinal + "-" + yearFinal);
}
}
}
【问题讨论】:
-
'日期搞砸了',你能详细说明一下吗?
-
例如,我将在 2018 年 1 月 27 日借一个项目。我应该在 2018 年 2 月 1 日归还它。它应该在您当前日期 +5 天。但我的程序生成 2018 年 2 月 32 日 @CowboyFarnz
标签: android date datepicker