我想创建具有
的微调器8:00-8:30 AM 8:30-9:00 AM 9:00-9:30上午
我使用ArrayList完成的时间段如
8:00 AM
9:00 AM
10:00 AM
等等。
我得到了像
这样的arraylistArrayList[08:00:AM, 09:00:AM, 10:00:AM, 11:00:AM, 12:00:PM, 13:00:PM, 14:00:PM, 15:00:PM, 16:00:PM, 17:00:PM, 18:00:PM, 19:00:PM, 20:00:PM, 21:00:PM]
这是我的代码:
public class MainActivity extends AppCompatActivity {
ArrayList<String> hours = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCustomTimeSpinner();
}
private void initCustomTimeSpinner() {
Spinner spinnerCustom = (Spinner) findViewById(R.id.spinner2);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:a");
SimpleDateFormat startHourFormat = new SimpleDateFormat("HH");
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
int startHour = Integer.parseInt(startHourFormat.format(cal.getTime()));
Log.e("startHour", Integer.toString(startHour));
Log.e("withot format Starttime", cal.getTime().toString());
SimpleDateFormat endHourFormat = new SimpleDateFormat("HH");
cal.set(Calendar.HOUR_OF_DAY, 21);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
int endHour = Integer.parseInt(endHourFormat.format(cal.getTime()));
Log.e("endHour", Integer.toString(endHour));
Log.e("withot format End time", cal.getTime().toString());
String test = sdf.format(cal.getTime());
Log.e("TEST", test);
for (int i = startHour; i <= endHour; i++) {
cal.set(Calendar.HOUR_OF_DAY, i);
hours.add(sdf.format(cal.getTime()));
System.out.println("ArrayList" + hours);
}
}
}
答案 0 :(得分:3)
以下是使用Java 8 Time API构建时隙的简单示例:
public static void printTimeSlots(LocalTime startTime, LocalTime endTime, int slotSizeInMinutes) {
for (LocalTime time = startTime, nextTime; time.isBefore(endTime); time = nextTime) {
nextTime = time.plusMinutes(slotSizeInMinutes);
if (nextTime.isAfter(endTime))
break; // time slot crosses end time
System.out.println(time + "-" + nextTime);
}
}
测试
printTimeSlots(LocalTime.parse("08:00"), LocalTime.parse("17:00"), 30);
输出
08:00-08:30
08:30-09:00
09:00-09:30
09:30-10:00
10:00-10:30
10:30-11:00
11:00-11:30
11:30-12:00
12:00-12:30
12:30-13:00
13:00-13:30
13:30-14:00
14:00-14:30
14:30-15:00
15:00-15:30
15:30-16:00
16:00-16:30
16:30-17:00
测试#2,显示未对齐的结束时间
printTimeSlots(LocalTime.parse("10:05"), LocalTime.parse("11:55"), 15);
10:05-10:20
10:20-10:35
10:35-10:50
10:50-11:05
11:05-11:20
11:20-11:35
11:35-11:50
当然,如果您不想在几分钟内限制广告位尺寸,则可以使用TemporalAmount
而不是分钟数:
public static void printTimeSlots(LocalTime startTime, LocalTime endTime, TemporalAmount amountToAdd) {
for (LocalTime time = startTime, nextTime; time.isBefore(endTime); time = nextTime) {
nextTime = time.plus(amountToAdd);
if (nextTime.isAfter(endTime))
break; // time slot crosses end time
System.out.println(time + "-" + nextTime);
}
}
测试
printTimeSlots(LocalTime.parse("10:05:15"),
LocalTime.parse("11:55:00"),
Duration.ofMinutes(15).plusSeconds(25));
10:05:15-10:20:40
10:20:40-10:36:05
10:36:05-10:51:30
10:51:30-11:06:55
11:06:55-11:22:20
11:22:20-11:37:45
11:37:45-11:53:10