我试图从一个由四个输入构建的Calendar对象中获得一分钟的时间:月份(第3天),白天(星期四),小时(1),分钟(30)和上午/下午.此外,如果时间是过去的一个月应该增加一个.
如何设置日历到这个时间?
这段代码似乎没有设定这一天.虽然小时,分钟和上午/下午设置正确.
日历设置:
long getTime() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, getDayOfWeekInMonth());
calendar.set(Calendar.DAY_OF_WEEK, getDayOfWeek());
calendar.set(Calendar.HOUR, getHour());
calendar.set(Calendar.MINUTE, getMinute());
calendar.set(Calendar.AM_PM, getAmPm());
if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
calendar.add(Calendar.MONTH, 1);
}
return calendar.getTimeInMillis();
}
int getDayOfWeekInMonth() {
return weekOfMonthSpinner.getSelectedItemPosition() + 1;
}
int getDayOfWeek() {
switch(dayOfWeekSpinner.getSelectedItemPosition()) {
case 0:
return Calendar.SUNDAY;
case 1:
return Calendar.MONDAY;
case 2:
return Calendar.TUESDAY;
case 3:
return Calendar.WEDNESDAY;
case 4:
return Calendar.THURSDAY;
case 5:
return Calendar.FRIDAY;
case 6:
return Calendar.SATURDAY;
default:
return 0;
}
}
int getHour() {
return Integer.parseInt((String) hourSpinner.getSelectedItem());
}
int getMinute() {
return Integer.parseInt((String) minuteSpinner.getSelectedItem());
}
int getAmPm() {
switch(amPmSpinner.getSelectedItemPosition()) {
case 0:
return Calendar.AM;
case 1:
return Calendar.PM;
default:
return 0;
}
}
微调器设置:
1st
2nd
3rd
4th
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
1
2
3
4
5
6
7
8
9
10
11
12
00
05
10
15
20
25
30
35
40
45
50
55
am
pm
adapter = ArrayAdapter.createFromResource(getActivity(), R.array.week_of_month, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
weekOfMonthSpinner.setAdapter(adapter);
adapter = ArrayAdapter.createFromResource(getActivity(), R.array.day_of_week, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
dayOfWeekSpinner.setAdapter(adapter);
adapter = ArrayAdapter.createFromResource(getActivity(), R.array.hour, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
hourSpinner.setAdapter(adapter);
adapter = ArrayAdapter.createFromResource(getActivity(), R.array.minute, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
minuteSpinner.setAdapter(adapter);
adapter = ArrayAdapter.createFromResource(getActivity(), R.array.am_pm, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
amPmSpinner.setAdapter(adapter);