首页 > 解决方案 > Android试图解析CalendarPickerView的日期范围

问题描述

我试图从日历选择器视图中获取一系列日期,然后将它们放入字符串列表中,最终放入 firebase firestore。我已经有一段时间了,但无法超越它的“解析”部分。我搜索了 stackO 并发现大多数答案仅与单个日期字符串有关,如此处所示我已经开始工作了。我只是对日期列表有疑问。

这就是我到目前为止所拥有的:

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);
}

任何帮助或指导表示赞赏。

标签: androidsimpledateformatandroid-calendar

解决方案


事实证明,我只需要一些睡眠......并且还将解析部分包含在 try/catch 块中。IDK为什么我昨晚没看到。无论如何,这就是我最终得到的

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();
                        }
``

推荐阅读