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

时间:2014-01-28 17:44:32

标签: java android time range

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

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范围内,它仍然无法计算。非常感谢任何帮助!

6 个答案:

答案 0 :(得分:3)

初始化变量并将方法的返回类型更改为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;
}

答案 1 :(得分:2)

在使用之前,您尚未初始化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;
}

答案 2 :(得分:1)

这个

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

不按照您的意愿行事。它的作用是将字段HOUR设置为Calendar.HOUR的值,这是一个任意常量。

您不需要这两行,因为getInstance会在当前时间返回一个日历。

答案 3 :(得分:1)

SimpleDateFormat format = new SimpleDateFormat(“MM / dd / yyyy HH:mm:ss”);         SimpleDateFormat sdf = new SimpleDateFormat(“MM / dd / yyyy”);

    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();
    }

答案 4 :(得分:0)

currentTime 置于 setTime 之外,并从 setTime 中删除 if-statement 。 如果您需要在 setTime 之外添加 if-statement

答案 5 :(得分:0)

 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;
    }


}