简体   繁体   中英

Android Studio java.time.format.DateTimeParseException: Text could not be parsed when picking date from date picker

I am trying to make an app for university for registering invoices. I have an Activity with a vendor class where I register them (code, name, phone number) and one Activity for registering invoices (code, series, date, explanations and vendor)

For inputting the date I used a TextView that when tapped, another dialog Activity opens with a DatePicker .

My problem is that when I press Submit, the app crashes and the following exception is triggered in the debug:

java.time.format.DateTimeParseException: Text '2021-11-9' could not be parsed at index 8

This is the code for the date class, save button and calendar dialog fragment:

private LocalDate date;

public void setDate(LocalDate d) {
    date = d;
}

public LocalDate getDate() {
    return date;
}

public TextView date;
private Invoice invoice;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_invoice);
    date = findViewById(R.id.idDate);

    Button btnSave = findViewById(R.id.btnSaveInvoice);
        
    data.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            calendarDialog calendar = new calendarDialog();
            calendar.show(getFragmentManager(),"calendarDialog");
        }
    });

    btnSave.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onClick(View v) {
            invoice = new Invoice();
            LocalDate date = LocalDate.parse(data.getText());
            invoice(date);
        }
    });
}
public class calendarDialog extends DialogFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,     @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(layout.calendar_my_dialog,container,false);
        CalendarView      calendarView=view.findViewById(id.calendarView);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int Year, int Month, int dayOfMonth) {
              int month = Month+1;
              String txtDate = Year+"-"+Month+"-"+dayOfMonth;
                ((activity_invoice)getActivity()).date.setText(txtDate);
                getDialog().dismiss();
            }
        });
        return view;
    }
}

You receive String s like "2021-11-9" , which are of the format "uuuu-MM-d" but when you try to parse them with LocalDate date = LocalDate.parse(data.getText()); you are implicitly using the standard ISO format, which has the pattern "uuuu-MM-dd" and is, therefore, expecting a String like "2021-11-09" (two digits for day of month). Your ones are missing a digit, at least when the day of month is less than 10.

You can pass a formatter that handles the single-digit format:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-d");

and then parse the String using that formatter:

LocalDate date = LocalDate.parse(data.getText(), dtf);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM