我有一个包含学生 ID 的所有类(class)的 cell[]
数组,每个单元格都有两个与此上下文相关的变量:
int day
和 int startTime
。
我还有两个给定的 currentDay
和 currentTime
值,它们的格式与单元格的日期和 startTime
我想创建一个循环来查找包含下一个类的单元格元素。
我尝试循环遍历元胞数组并选择包含至少一个类的最近一天,并取得了一些成功,我想我需要创建另一个包含当天所有类的数组,然后执行以下操作对他们来说同样的逻辑。我就是不知道怎么办。
public Cell getNextClass(int studentID, Context context){
DBHelper dbHelper = new DBHelper(context);
Cell[] cell = dbHelper.queryCellData(studentID);
Date currentTime = Calendar.getInstance().getTime();
int currentDay = getCurrentDay(currentTime.toString());
Log.d(TAG, "currentDay: " + currentDay);
DateFormat dateFormat = new SimpleDateFormat("hh a");
String formattedDate= dateFormat.format(currentTime);
int currentHour = getCurrentHour(formattedDate);
//TODO Find next class given currentDay and currentHour
Cell nextClass = cell[0];
for (int i = 0; i < cell.length; i++) {
if (cell[i].getDay() >= currentDay && cell[i].getDay() <= nextClass.getDay()){
//?????
}
}
return nextClass;
}
}
在此示例中,cell[0]
在 12 小时有一个类,并且默认显示,因为循环不会更改任何内容。然而,在新西兰发布时,值将是:
currentDay = 2
currentHour = 21
如我的时间表截图所示:i.imgur.com/X6HzR6y.png
下一个类将于明天第 3 天第 8 小时进行。
最佳答案
我建议您对 Cell
类的字段使用 java.time 类,而不是 int
:
public class Cell {
DayOfWeek day;
LocalTime startTime;
// Constructor, getters, toString, etc.
}
此外,如果可以的话,请使用一种方法来适应您的类(class),该方法给定一周中的某一天和时间,计算距下次类(class)开始还有多长时间:
public Duration getTimeUntilNext(DayOfWeek currentDay, LocalTime currentTime) {
int daysUntil = day.getValue() - currentDay.getValue();
Duration time = Duration.ofDays(daysUntil).plus(Duration.between(currentTime, startTime));
if (time.isNegative()) { // over for this week
// Calculate time until next week’s occurrence
time = time.plusDays(7);
assert ! time.isNegative() : time;
}
return time;
}
如果您无法将此方法添加到 Cell
类中,您可以在其他地方将其声明为静态方法,然后将单元格添加为额外参数。
现在剩下的还不错:
Cell[] classes = new Cell[] {
new Cell(DayOfWeek.THURSDAY, LocalTime.of(12, 0)),
new Cell(DayOfWeek.MONDAY, LocalTime.of(14, 0)),
new Cell(DayOfWeek.THURSDAY, LocalTime.of(10, 0)),
new Cell(DayOfWeek.FRIDAY, LocalTime.of(9, 0)),
new Cell(DayOfWeek.THURSDAY, LocalTime.of(6, 0))
};
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Pacific/Auckland"));
final DayOfWeek currentDay = now.getDayOfWeek();
final LocalTime currentTime = now.toLocalTime();
Comparator<Cell> comparatorByTimeUntilNext = new Comparator<Cell>() {
@Override
public int compare(Cell c1, Cell c2) {
return c1.getTimeUntilNext(currentDay, currentTime)
.compareTo(c2.getTimeUntilNext(currentDay, currentTime));
}
};
Cell nextClass = Collections.min(Arrays.asList(classes), comparatorByTimeUntilNext);
System.out.println("Next class from " + now + " is " + nextClass);
当我刚才运行它时,它输出:
Next class from 2019-06-20T06:49:23.188+12:00[Pacific/Auckland] is THURSDAY at 10:00
即使您坚持在类中使用 int
字段,您仍然应该能够使用这个想法。然后,getTimeUntilNext
可能仍然返回 Duration
或表示小时数的 int
。当然,我推荐前者。
在我看来,您的代码将当前日期和时间格式化为两个不同的字符串,然后解析它们以获取日期和时间。这肯定是走了弯路。另外,正如我在评论中所说,我建议您避免 Date
、Calendar
、DateFormat
和 SimpleDateFormat
因为它们全部设计得很糟糕并且已经过时了。
问题:我可以在 Android 上使用 java.time 吗?
是的,java.time 在旧版和新版 Android 设备上都能很好地工作。它只需要至少 Java 6。
- 在 Java 8 及更高版本以及较新的 Android 设备(从 API 级别 26 开始)中,现代 API 是内置的。
- 在 Java 6 和 7 中获取 ThreeTen Backport,即现代类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
- 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保使用子包从
org. Threeten.bp
导入日期和时间类。
链接
- Oracle tutorial: Date Time解释如何使用 java.time。
- Java Specification Request (JSR) 310 ,其中首次描述了 java.time。
- ThreeTen Backport project ,
java.time
向 Java 6 和 7 的向后移植(对于 JSR-310,为 ThreeTen)。 - ThreeTenABP , Android 版 ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project ,有非常详尽的解释。
关于java - 给定日期和时间值,我如何在时间表中找到下一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56664533/