Java 给定一天和时间值,我如何在时间表中找到下一节课?

Java 给定一天和时间值,我如何在时间表中找到下一节课?,java,android,Java,Android,我有一个单元格[]数组,包含学生ID的所有类,每个单元格有两个与此上下文相关的变量: int-day和int-startTime。 我还为currentDay和currentdime提供了两个给定值,这两个值的格式与单元格的day和startTime相同 我想做一个循环,查找包含下一个类的cell元素 我试着在单元格数组中循环,选择最近的一天,其中至少包含一个类,取得了一些成功,我想我需要创建另一个数组,其中包含当天的所有类,然后对它们执行相同的逻辑。我就是不知道怎么做 public Cell

我有一个
单元格[]
数组,包含学生ID的所有类,每个单元格有两个与此上下文相关的变量:
int-day
int-startTime
。 我还为
currentDay
currentdime
提供了两个给定值,这两个值的格式与单元格的day和startTime相同

我想做一个循环,查找包含下一个类的cell元素

我试着在单元格数组中循环,选择最近的一天,其中至少包含一个类,取得了一些成功,我想我需要创建另一个数组,其中包含当天的所有类,然后对它们执行相同的逻辑。我就是不知道怎么做

public Cell getNextClass(int studentID,Context){
DBHelper DBHelper=新的DBHelper(上下文);
Cell[]Cell=dbHelper.queryCellData(studentID);
Date currentTime=Calendar.getInstance().getTime();
int currentDay=getCurrentDay(currentTime.toString());
Log.d(标签“currentDay:+currentDay”);
DateFormat DateFormat=新的SimpleDateFormat(“hh a”);
字符串formattedDate=dateFormat.format(当前时间);
int currentHour=getCurrentHour(formattedDate);
//TODO查找给定currentDay和currentHour的下一个类
单元nextClass=单元[0];
for(int i=0;i如果(cell[i].getDay()>=currentDay&&cell[i].getDay()这是一个很好的切入点:


我知道您不想更改代码。但是,由于您的应用程序处理日期和时间,它应该使用内置api,
java
语言提供给您的。请尝试以一种受控和可测试的方式引入它。
String类
也很庞大,但我打赌您使用的是该类,而不是您自己的实现n、 这里是一个很好的切入点:


我知道您不想更改代码。但是,由于您的应用程序处理日期和时间,它应该使用内置api,
java
语言提供给您的。请尝试以一种受控和可测试的方式引入它。
String类
也很庞大,但我打赌您使用的是该类,而不是您自己的实现n、 谢谢你的推荐,@Jocke然而,我找到了自己的方法(很糟糕),因此,为了子孙后代,也为了那些把自己编码成和我一样处境的穷学生,我刚刚添加了一些关于时间如何在循环周场景(如时间表)中工作的混乱边缘案例。将来可能的话,我将使用默认库。别担心。这是我使用的最后一种方法

public Cell getNextClass(int studentID,Context){
DBHelper DBHelper=新的DBHelper(上下文);
Cell[]Cell=dbHelper.queryCellData(studentID);
Date currentTime=Calendar.getInstance().getTime();
int currentDay=getCurrentDay(currentTime.toString());
Log.d(标签“currentDay:+currentDay”);
DateFormat DateFormat=新的SimpleDateFormat(“hh a”);
字符串formattedDate=dateFormat.format(当前时间);
int currentHour=getCurrentHour(formattedDate);
ArrayList classesInDay=新的ArrayList();
如果(当前小时>=17){
currentDay++;//如果当前小时已过一天的结束时间,则在当天添加1,以便扫描明天。
}
如果(当前日期>4){
currentDay=0;//如果当前日期大于4(周末),则将日期设置为星期一。
}
for(int i=0;i4){
currentDay=0;//如果当前日期大于4(周末),则将日期设置为星期一。
}
if(单元格[i].getDay()==currentDay){
Log.d(标记“getNextClass:cell”+i+“今天有一个类”);
添加(单元格[i]);
}
}
nextClass=classesInDay.get(0);//为新的一天重新应用默认类
}
对于(int i=1;i<(classesInDay.size());i++){
int diff1=classesInDay.get(i).getStartTime()-currentHour-1;//减去一以确保找到的是下一个类,而不是当前类。
int diff2=nextClass.getStartTime()-currentHour-1;
Log.d(标签“diff1:+diff1+”diff2:+diff2);
如果(diff1<0){//这意味着测试单元在当前小时之前
}如果(diff2<0){//则表示当前选择在当前小时之前
nextClass=classesInDay.get(i);
}否则,如果(diff1
谢谢你的推荐,@Jocke然而,我找到了自己的方法(很糟糕),因此,为了子孙后代,也为了那些把自己编码成和我一样处境的穷学生,我刚刚添加了一些关于时间如何在循环周场景(如时间表)中工作的混乱边缘案例。将来可能的话,我将使用默认库。别担心。这是我使用的最后一种方法

public Cell getNextClass(int studentID,Con
    currentDay = 2
    currentHour = 21
public class Cell {
    DayOfWeek day;
    LocalTime startTime;

    // Constructor, getters, toString, etc.
}
    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[] 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);