I am trying to implement something similar to a timesheet so what I basically need is when I select one particular date from the datepicker dialog then I want to display a textview which contains the days of the whole week in which the selected date is present.
Below is what I have implemented to select a date from the date picket dialog.
Calendar cal = Calendar.getInstance();
EditText edt1;
String dateFormat = "dd/MM/yyyy";
final DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel1();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timesheet_form);
edt1=(EditText)findViewById(R.id.editText1);
edt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(TimesheetForm.this, date1, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabel1() {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US);
edt1.setText(sdf.format(fromCalendar.getTime()));
}
The above code returns the selected date from the dialog but there seems to be no method to return the days of the whole week.
The code below is an example to get the days of the current/selected week. In my case I've set the date hardcoded to test the functionality.
Calendar cal = Calendar.getInstance();
// Set a custom date : 2013 November 17th
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.MONTH, 10);
cal.set(Calendar.DATE, 17);
// Date to an int variable to display
int date = cal.get(Calendar.DATE);
// Display the week number
int weekNr = cal.get(Calendar.WEEK_OF_YEAR);
// Generate the String with the days of the current week
StringBuffer weekDays = new StringBuffer();
for (int i = 1; i <= 7; i++) {
cal.set(Calendar.DAY_OF_WEEK, i);
weekDays.append(cal.get(Calendar.DATE));
if (i < 7) {
weekDays.append(" :: ");
}
}
// Show an alert
showAlert("Year: " + cal.get(Calendar.YEAR) + "\nMonth : " + (cal.get(Calendar.MONTH) + 1) +
"\nDay: " + date + "\n\nWeek : " + weekNr + "\n\nWeek Days : " + weekDays.toString());
and here the method of the alert:
public void showAlert(String message) {
new AlertDialog.Builder(this).setTitle("Date").setMessage(message).setCancelable(true).show();
}
You have to modify this a little bit, so that this fits in your code. Best practice is to create a separate method for this or add it in your updateLabel1() method and call that method from your onDateSetListener from your DatePicker. Keep in mind that your week always starts on Sunday.
This is the result I get from this code:
Good luck,
Kr
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.