Hey guys i am working on an application that can be used to predict the due date of a babies birth but i have failed to figure out how to increment the date from the date time picker dialog below is my code
public class Dday extends Activity implements OnClickListener{
private EditText fromDateEtxt;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dday);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
findViewsById();
setDateTimeField();
}
private void findViewsById() {
fromDateEtxt = (EditText) findViewById(R.id.etxt_fromdate);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();
}
private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dday, menu);
return true;
}
public void showDate (View view)
{
// Adding the days to the date
// Button action
Intent dts = new Intent(this,Details.class);
startActivity(dts);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(view == fromDateEtxt) {
fromDatePickerDialog.show();
}
}
You can increment a Calendar object by 39 weeks with something like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_YEAR, 39);
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
Calendar c = Calendar.getInstance();
c.set(selectedYear, selectedMonth, selectedDay);
c.add(Calendar.DATE, 39);
}
for more see this thread How do I set the date for a date picker?
You can use the Calendar Object and create a date from Today and add 39 weeks using the Calendar class by setting Date objects based on the Calendar and modified Calendar like so:
// Formatter for readable comparison
DateFormat sdf = new SimpleDateFormat("MM dd yyyy", Locale.getDefault());
// Today
Calendar now = Calendar.getInstance();
Date dateToday = new Date();
dateToday.setTime(now.getTimeInMillis());
// 39 weeks from today
Date futureDate = new Date();
// Calendar supports roll over with year so no need to worry about modifying year separate
now.add(Calendar.WEEK_OF_YEAR, 39);
futureDate.setTime(now.getTimeInMillis());
// Print out for comparison
System.out.println(sdf.format(dateToday));
System.out.println(sdf.format(futureDate));
Which prints out:
I/System.out﹕ 09 16 2015 <-- Today
I/System.out﹕ 06 15 2016 <-- 39 weeks later
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.