0

I have a DatePickerFragment that lets the user select a date from a calendar. The captured date is then set on an EditText line. The below code in onDateSet worked fine with a prior version of the OS now it gives the below error and date is not showing up as expected: mm/dd/yyyy. The error is highlighted on the "txtDate.setText..." line. What am I doing wrong here?

enter image description here

public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);

    // If the argsbundle from the Activity has data (because the user previously selected a date) then
    // set that date in the DatePicker.
    if (getArguments() != null) {
        c = Calendar.getInstance();
        year = getArguments().getInt("year");
        month = getArguments().getInt("month");
        day = getArguments().getInt("day");
        c.set(year, month, day);
    } else { // If the DueDate EditText line is empty (no previously selected date by the user then
        // set today's date into the DatePicker.
        // Calendar class obtains the current date on the device and has fields for
        // each of the parts of the date: day, month and year.
        c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
    }

    DatePickerDialog picker = new DatePickerDialog(getActivity(),
            this, year, month, day);
    // Set monthly calendar rather than default spinner.
    picker.getDatePicker().setCalendarViewShown(true);
    // Turn off date selector spinner.
    picker.getDatePicker().setSpinnersShown(false);
    picker.setTitle("Select a Due Date");

    return picker;
}

partial DatePickerFragment.java file:

...
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
     // Set the selected date into the FEditText line.
     EditText txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
     // Format the selected date.
     txtDate.setText((monthOfYear + 1) + "/" + dayOfMonth + "/" + year + " ");
     // Display the date.  "Month" uses a zero-based index from 0 to 11, so need to add 1 to show properly.
     txtDate.setSelection(txtDate.getText().length());
}
AJW
  • 1,578
  • 3
  • 36
  • 77
  • This is a warning and it is pretty clear what it is saying. 1) String concatenation with numbers may not yield intended results, and 2) You should use a string resource via res/strings.xml – OneCricketeer Oct 22 '15 at 00:23
  • I'm new to programming and Android...it wasn't clear to me, which is why I asked the question. – AJW Oct 22 '15 at 00:31

1 Answers1

2

You can do :

 String date = String.format("%d/%d/%d ",monthOfYear + 1, dayOfMonth, year);
 txtDate.setText(date);

Or:

int day = view.getDayOfMonth();
int month = view.getMonth();
int year =  view.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, day);

Date asDate = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy", Locale.US);
txtDate.setText(sdf.format(asDate));
jyanks
  • 2,356
  • 1
  • 19
  • 36
  • Ok, I'll try that. I'm new to Andr. programming, what is %d? – AJW Oct 22 '15 at 00:16
  • String.format() is a helper method that will replace the `%d` or `%s` characters with the arguments that are after the string argument. See [this link](http://developer.android.com/reference/java/util/Formatter.html) – jyanks Oct 22 '15 at 00:18
  • Ok very cool. When I run the String date code I end up with only the year showing and no day or month (e.g., 15 for 2015). When I replace "year" with "year-2000" I get 10/21/15 but I'm looking for 10/21/2015. – AJW Oct 22 '15 at 00:24
  • Ok. I've read a bunch of conflicting online posts that say SimpleDateFormat may cause problems because it may not be thread safe. Do you have any thoughts on this? – AJW Oct 22 '15 at 00:36
  • If you're doing the simple date format in the `onDateSet` handler, then you shouldn't have to worry about thread safety. I would switch to use joda date which does have a date time formatter that is threadsafe (and the code is cleaner). – jyanks Oct 22 '15 at 00:38
  • Ok I'll look into that. One more question: is there any way to have the date be formatted by picking up the device's Locale rather than always setting to US Locale? – AJW Oct 22 '15 at 00:41
  • Excellent, thanks. And answer has been upvoted and accepted, cheers. – AJW Oct 22 '15 at 00:46
  • Minor fix needed. When I changed to the SimpleDateFormat code you recommended I am now getting the same year error where it only shows 15 and that is all it shows. When I switch to "yy" it shows as correctly as 10/21/15 but I am trying to show 10/21/2015. Any thoughts on how to fix? – AJW Oct 22 '15 at 01:04
  • Also, using the SimpleDateFormat code with OnDateSet is now just putting the default date (today's date) on the EditText line, not the date the user is selecting...any ideas on how to fix? – AJW Oct 22 '15 at 01:16
  • Did you try using the date picker's getters for the year/month/day? – jyanks Oct 22 '15 at 01:20
  • Yes, see above where I've added onCreateDialog lines. – AJW Oct 22 '15 at 01:28
  • I think the error is with the c.getTime(). It is just getting today's date, not the previously selected date that the user picked in the DatePicker. – AJW Oct 22 '15 at 01:37