5

我试图从由四个输入构建的日历对象中将时间缩短到分钟:月(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;
    }
}

微调器设置:

<string-array name="week_of_month">
    <item>1st</item>
    <item>2nd</item>
    <item>3rd</item>
    <item>4th</item>
 </string-array>
<string-array name="day_of_week">
    <item>Sunday</item>
    <item>Monday</item>
    <item>Tuesday</item>
    <item>Wednesday</item>
    <item>Thursday</item>
    <item>Friday</item>
    <item>Saturday</item>
</string-array>
<string-array name="hour">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
</string-array>
<string-array name="minute">
    <item>00</item>
    <item>05</item>
    <item>10</item>
    <item>15</item>
    <item>20</item>
    <item>25</item>
    <item>30</item>
    <item>35</item>
    <item>40</item>
    <item>45</item>
    <item>50</item>
    <item>55</item>
</string-array>
<string-array name="am_pm">
    <item>am</item>
    <item>pm</item>
</string-array>

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

1 回答 1

7

使用calendar.set(Calendar.WEEK_OF_MONTH, <week of the month>);calendar.set(Calendar.DAY_OF_WEEK, 4);

例子 -

Calendar calendar = Calendar.getInstance();
System.out.println("Before - "+calendar.getTime());
calendar.set(Calendar.WEEK_OF_MONTH, 3);
calendar.set(Calendar.DAY_OF_WEEK, 4);
System.out.println("After - "+calendar.getTime());

结果 -

Before - Tue Feb 12 23:23:28 IST 2013
After - Wed Feb 13 23:18:16 IST 2013
于 2013-02-12T09:04:02.987 回答