我在我的Android应用程序中有一个列表视图,其中我显示间隔30分钟的列表。我正在使用此函数将数据硬编码到列表视图中
public void prepareTime() {
ListModel listModel = new ListModel("00:30 A.M");
listModelsList.add(listModel);
listModel = new ListModel("01:00 A.M");
listModelsList.add(listModel);
listModel = new ListModel("01:30 A.M");
listModelsList.add(listModel);
listModel = new ListModel("02:00 A.M");
listModelsList.add(listModel);
listModel = new ListModel("02:30 A.M");
listModelsList.add(listModel);
listModel = new ListModel("03:00 A.M");
listModelsList.add(listModel);
listModel = new ListModel("03:30 A.M");
listModelsList.add(listModel);
listModel = new ListModel("04:00 A.M");
listModelsList.add(listModel);
listModel = new ListModel("04:30 A.M");
listModelsList.add(listModel);
listModel = new ListModel("05:00 A.M");
listModelsList.add(listModel);
listModel = new ListModel("05:30 A.M");
listModelsList.add(listModel);
listModel = new ListModel("06:00 A.M");
listModelsList.add(listModel);
listModel = new ListModel("06:30 A.M");
listModelsList.add(listModel);
ListsAdapter listAdapter = new ListsAdapter(getApplicationContext(), listModelsList);
list.setAdapter(listAdapter);
}
但我想使用移动时区以30分钟的间隔将数据添加到列表中。 我怎样才能做到这一点?
答案 0 :(得分:0)
搜索结束后,我得到了问题的答案。
private void displayTimeSlots() {
String timeValue = "T00:00:4.896+05:30";
SimpleDateFormat sdf = new SimpleDateFormat("'T'hh:mm:ss.SSS");
try {
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(sdf.parse(timeValue));
if (startCalendar.get(Calendar.MINUTE) < 30) {
startCalendar.set(Calendar.MINUTE, 30);
} else {
startCalendar.add(Calendar.MINUTE, 30); // overstep hour and clear minutes
startCalendar.clear(Calendar.MINUTE);
}
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(startCalendar.getTime());
// if you want dates for whole next day, uncomment next line
//endCalendar.add(Calendar.DAY_OF_YEAR, 1);
endCalendar.add(Calendar.HOUR_OF_DAY, 24 - startCalendar.get(Calendar.HOUR_OF_DAY));
endCalendar.clear(Calendar.MINUTE);
endCalendar.clear(Calendar.SECOND);
endCalendar.clear(Calendar.MILLISECOND);
SimpleDateFormat slotTime = new SimpleDateFormat("hh:mm a");
while (endCalendar.after(startCalendar)) {
startCalendar.add(Calendar.MINUTE, 30);
String slotEndTime = slotTime.format(startCalendar.getTime());
ListModel listModel = new ListModel(slotEndTime);
listModelsList.add(listModel);
ListsAdapter listAdapter = new ListsAdapter(getApplicationContext(), listModelsList);
list.setAdapter(listAdapter);
Log.e("DATE", slotEndTime);
}
} catch (ParseException e) {
// date in wrong format
}
}