0

I am trying to grab a range of dates from a calendar picker view and then put them into a string list to eventually be put into firebase firestore. I've been at this for some time but cannot get past the "parse" part of it. I've searched stackO and found most answers only pertain to a single date string as shown here which I have gotten to work. I'm just having issues with a list of dates.

this is what I have so far:

CalendarPickerView bookProfileCalendar;
DateFormat dateRangeInputFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);
SimpleDateFormat dateRangeOutputFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

List<Date> testListDate = bookProfileCalendar.getSelectedDates();

for (Date x: testListDate){ 
    String selectedDatesFormatted = dateRangeInputFormat.format(x); //this allows the input to be read as a date in its default format
    String test = String.valueOf(selectedDatesFormatted); //not sure this is necessary
//  Date test2 = dateRangeInputFormat.parse(test); // <-- this gives me parse underlined red saying "unhandled exception: java.text.ParseException
    Log.d(TAG, "onClick: testList dates: " + test);
}

any help or guidance is appreciated.

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
Cflux
  • 1,423
  • 3
  • 19
  • 39
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Nov 19 '19 at 20:35

1 Answers1

0

It turns out I just needed some sleep...and to also surround the parse part in a try/catch block. IDK why I wasn't seeing that last night. Anyhow, this is what I ended up with

List<Date> testListDate = bookProfileCalendar.getSelectedDates();


                    for (Date x: testListDate){ // preferred way of doing for loops now!!!
                        String selectedDatesFormatted = dateRangeInputFormat.format(x);
                        String test = String.valueOf(selectedDatesFormatted);

                        try {
                            Date test2 = dateRangeInputFormat.parse(test);
                            String test3 = dateRangeOutputFormat2.format(test2);
                            Log.d(TAG, "onClick: test3: " + test3);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
``
Cflux
  • 1,423
  • 3
  • 19
  • 39