检查时间是否介于Android中的时间范围之间

fjnneemd  于 2022-12-09  发布在  Android
关注(0)|答案(6)|浏览(269)

我试着检查当前时间是否在指定的范围内。我做了一个方法来检查这个,但是它不起作用。我不知道为什么不起作用,也不知道如何让它起作用。

private Calendar fromTime;
private Calendar toTime;
private Calendar currentTime;

public boolean checkTime(String time) {
    try {
        String[] times = time.split("-");
        String[] from = times[0].split(":");
        String[] until = times[1].split(":");

        fromTime = Calendar.getInstance();
        fromTime.set(Calendar.HOUR, Integer.valueOf(from[0]));
        fromTime.set(Calendar.MINUTE, Integer.valueOf(from[1]));

        toTime= Calendar.getInstance();
        toTime.set(Calendar.HOUR, Integer.valueOf(until[0]));
        toTime.set(Calendar.MINUTE, Integer.valueOf(until[1]));

        currentTime = Calendar.getInstance();       
        currentTime.set(Calendar.HOUR, Calendar.HOUR_OF_DAY);
        currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
        if(currentTime.after(fromTime) && currentTime.before(toTime)){
            return true;
        }
     } catch (Exception e) {
         return false;
     }
    return false;
}

我试着这样来检验它:

if(checkTime("06:00-19:00")){
    inRange = true;
}

NPE已经没有了,但是它还没有计算时间是否在fromTimetoTime的范围内。非常感谢您的帮助!

3mpgtkmj

3mpgtkmj1#

初始化变量并将方法的返回类型更改为boolean。

private Calendar fromTime;
private Calendar toTime;
private Calendar currentTime;

public boolean checkTime(String time) {
try {
    String[] times = time.split("-");
    String[] from = times[0].split(":");
    String[] until = times[1].split(":");

    fromTime = Calendar.getInstance();
    fromTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(from[0]));
    fromTime.set(Calendar.MINUTE, Integer.valueOf(from[1]));

    toTime = Calendar.getInstance();
    toTime.set(Calendar.HOUR_OF_DAY, Integer.valueOf(until[0]));
    toTime.set(Calendar.MINUTE, Integer.valueOf(until[1]));

    currentTime = Calendar.getInstance();
    currentTime.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
    currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
    if(currentTime.after(fromTime) && currentTime.before(toTime)){
        return true;
    }
    } catch (Exception e) {
        return false;
    }
    return false;
}
s4chpxco

s4chpxco2#

在使用toTime和fromTime对象之前,您尚未对其进行初始化。因此,最好调用toTime = Calendar.getInstance();之前。

private Calendar fromTime;
private Calendar toTime;
private Calendar currentTime;

public boolean checkTime(String time) {
try {
    String[] times = time.split("-");
    String[] from = times[0].split(":");
    String[] until = times[1].split(":");

    fromTime = Calendar.getInstance();
    fromTime.set(Calendar.HOUR, Integer.valueOf(from[0]));
    fromTime.set(Calendar.MINUTE, Integer.valueOf(from[1]));

    toTime = Calendar.getInstance();
    toTime.set(Calendar.HOUR, Integer.valueOf(until[0]));
    toTime.set(Calendar.MINUTE, Integer.valueOf(until[1]));

    currentTime = Calendar.getInstance();
    currentTime.set(Calendar.HOUR, Calendar.HOUR_OF_DAY);
    currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
    if(currentTime.after(fromTime) && currentTime.before(toTime)){
        return true;
    }
    } catch (Exception e) {
        return false;
    }
    return false;
}
dluptydi

dluptydi3#

这其中

currentTime.set(Calendar.HOUR, Calendar.HOUR_OF_DAY);
    currentTime.set(Calendar.MINUTE, Calendar.MINUTE);

不执行您希望它执行的操作。它所做的是将字段HOUR设置为Calendar.HOUR的值,这是一个任意常量。
您不需要这两行,因为getInstance返回当前时间的Calendar。

j0pj023g

j0pj023g4#

简单日期格式格式=新的简单日期格式(“年/月:日时:分:秒”);简单日期格式sdf =新的简单日期格式(“年/月/日”);

Calendar cal = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();


    //cal.add(Calendar.DATE, 1);
    String date = sdf.format(cal.getTime());
    String dateStart = date+" "+"11:30:00";

    String date2 = sdf.format(cal2.getTime());
    String dateStop =  date2+" "+"23:00:00";

    Calendar calendar = Calendar.getInstance();
    String currentTime =  format.format(calendar.getTime());


    Date d1 = null;
    Date d2 = null;
    Date d3 = null;

    try {
        d1 = format.parse(dateStart);

    d2 = format.parse(dateStop);
        d3 = format.parse(currentTime);
       // Toast.makeText(getApplicationContext(),""+dateCal,Toast.LENGTH_SHORT).show();
        if (d3.before(d2)
                && d3.after(d1) ){}else{} } catch (ParseException e) {
        e.printStackTrace();
    }
gpnt7bae

gpnt7bae5#

currentTime置于setTime之外,并从setTime中删除***if语句***。如果需要,请将*if语句*置于setTime之外。

daolsyd0

daolsyd06#

private boolean checkBedTime(String time) {
    // Time Pattern Like : "06:00-19:00"
    String[] times = time.split("-");
    String[] from = times[0].split(":");
    String[] until = times[1].split(":");

    int fromHour = Integer.parseInt(from[0]);
    int fromMinute = Integer.parseInt(from[1]);

    int toHour = Integer.parseInt(until[0]);
    int toMinute = Integer.parseInt(until[1]);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
    int currentMinute = calendar.get(Calendar.MINUTE);

    int convertedFromMinute = (fromHour * 60) + fromMinute;
    int convertedToMinute = (toHour * 60) + toMinute;
    int convertedCurrentMinute = (currentHour * 60) + currentMinute;

    if (convertedFromMinute == convertedToMinute) {
        Toast.makeText(context, "Sleep Time & Wake Up Time can't be same", Toast.LENGTH_SHORT).show();
        return false;

    } else if (convertedToMinute < convertedFromMinute) {
        convertedToMinute = convertedToMinute + (24 * 60);
    }
    Log.v("Time", "FromMinute --> " + convertedFromMinute);
    Log.v("Time", "ToMinute --> " + convertedToMinute);
    Log.v("Time", "CurrentMinute -- > " + convertedCurrentMinute);
    if (convertedCurrentMinute >= convertedFromMinute && convertedCurrentMinute <= convertedToMinute) {
        return true;
    } else {
        return false;
    }

}

相关问题