android - 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);
}
任何帮助或指导表示赞赏。
解决方案
事实证明,我只需要一些睡眠......并且还将解析部分包含在 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();
}
``
推荐阅读
- c# - .net core mvc 表单验证中的选择时必需的属性不起作用?
- react-native - 未找到 react-native-jitsi-meet 模块
- azure - 与 Azure AKS 群集主节点 REST API 交互
- r - 如何对 R 中的列求和,其中要求和的列在单独的数据框中定义
- asp.net-core - 在.net core web api中返回带有异常详细信息的自定义对象
- php - 在 Composer 中配置类自动加载
- angular - Angular - 如何检查回调的下一个值是否是特定的,如果不是,则做些什么?
- c# - 从初始百分比中减去,但仅当用户输入“否”/假 C#
- css - 使用 Z-index 定位 DIV 和元素
- java - java GeoTools:按特征区域对 FeatureIterator 进行排序