例如,我正在尝试创建一个从当前时间到设置时间的时间数组;当前时间是15:41,我希望将其四舍五入到最近的一刻钟(15:45),并从15:45到指定时间创建一个15分钟间隔的数组,可以说23: 30。
我已经设法以15分钟为间隔,在24小时内创建了一系列时间,而且似乎无法进一步推进。
String[] quarterHours = {"00","15","30","45"};
String[] times = new String[24];
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times[i] = "Today " + time;
}
}
上面在Android的DialogList视图中的输出如下:
答案 0 :(得分:3)
您要覆盖内部循环的每个周期的时间。您应该改用List<String>
,而不必担心索引,只需添加即可,如下所示:
String[] quarterHours = {"00","15","30","45"};
List<String> times = new ArrayList<String>; // <-- List instead of array
for(int i = 0; i < 24; i++) {
for(int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if(i < 10) {
time = "0" + time;
}
times.add("Today " + time); // <-- no need to care about indexes
}
}
答案 1 :(得分:1)
使用内置的日期/时间API(特别是LocalTime
)会容易得多:
LocalTime time = LocalTime.now();
int minute = time.getMinute();
if (minute > 45) {
time = time.plusHours(1L).withMinute(0);
} else {
time = time.withMinute(minute < 30 ? minute < 15 ? 15 : 30: 45);
}
Stream.iterate(time.truncatedTo(ChronoUnit.MINUTES), t -> t.plusMinutes(15L))
.limit(5)
.forEach(System.out::println);
上面的代码输出以下内容:
11:00
11:15
11:30
11:45
12:00
您的工作将只是弄清楚数学以限制Stream
的大小。
答案 2 :(得分:0)
尝试一下。在这种情况下,用times[i]
将项目插入数组是不正确的。它只会计算第一个循环项
public class Main
{
public static void main(String[] args)
{
String[] quarterHours = { "00", "15", "30", "45" };
String[] times = new String[96];
int count = 0;
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 4; j++)
{
String time = i + ":" + quarterHours[j];
if (i < 10)
{
time = "0" + time;
}
times[count] = "Today " + time;
count++;
}
}
for (int i = 0; i < times.length; i++)
{
System.out.println(times[i]);
}
}
}
答案 3 :(得分:0)
以前的方法存在的问题是最后一分钟胜出并覆盖了之前的任何结果。
我建议使用List
,它更容易处理。您不需要照顾大小。
String[] quarterHours = { "00", "15", "30", "45" };
List<String> times = new ArrayList<>();
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 4; j++) {
String time = i + ":" + quarterHours[j];
if (i < 10) {
time = "0" + time;
}
times.add("Today " + time);
}
}
System.out.println(times);
输出:
[Today 00:00, Today 00:15, Today 00:30, Today 00:45, Today 01:00, Today 01:15, Today 01:30, Today 01:45, Today 02:00, Today 02:15, Today 02:30, Today 02:45, Today 03:00, Today 03:15, Today 03:30, Today 03:45, Today 04:00, Today 04:15, Today 04:30, Today 04:45, Today 05:00, Today 05:15, Today 05:30, Today 05:45, Today 06:00, Today 06:15, Today 06:30, Today 06:45, Today 07:00, Today 07:15, Today 07:30, Today 07:45, Today 08:00, Today 08:15, Today 08:30, Today 08:45, Today 09:00, Today 09:15, Today 09:30, Today 09:45, Today 10:00, Today 10:15, Today 10:30, Today 10:45, Today 11:00, Today 11:15, Today 11:30, Today 11:45, Today 12:00, Today 12:15, Today 12:30, Today 12:45, Today 13:00, Today 13:15, Today 13:30, Today 13:45, Today 14:00, Today 14:15, Today 14:30, Today 14:45, Today 15:00, Today 15:15, Today 15:30, Today 15:45, Today 16:00, Today 16:15, Today 16:30, Today 16:45, Today 17:00, Today 17:15, Today 17:30, Today 17:45, Today 18:00, Today 18:15, Today 18:30, Today 18:45, Today 19:00, Today 19:15, Today 19:30, Today 19:45, Today 20:00, Today 20:15, Today 20:30, Today 20:45, Today 21:00, Today 21:15, Today 21:30, Today 21:45, Today 22:00, Today 22:15, Today 22:30, Today 22:45, Today 23:00, Today 23:15, Today 23:30, Today 23:45]