具有完美字符串的SimpleDateFormat异常

时间:2017-11-06 10:38:13

标签: java android exception simpledateformat

以下代码给出了这个例外:

PID: 8473                                                                
java.lang.IllegalArgumentException: Not a valid time, expecting HH:MM:SS format

我要做的是检查给定时间是否介于两次之间。 我设法创建了一个简单的方法,但是当我试图检查之间它们出错时就说20:00 - 04:00,所以一旦它通过00:00就错了,所以我来到这里发现:

public static boolean isTimeBetweenTwoTime(String argStartTime,
                                           String argEndTime, String argCurrentTime) throws ParseException {
    String reg = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
    //
    if (argStartTime.matches(reg) && argEndTime.matches(reg)
            && argCurrentTime.matches(reg)) {
        boolean valid = false;
        // Start Time
        java.util.Date startTime = new SimpleDateFormat("HH:mm:ss")
                .parse(argStartTime);
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);

        // Current Time
        java.util.Date currentTime = new SimpleDateFormat("HH:mm:ss")
                .parse(argCurrentTime);
        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.setTime(currentTime);

        // End Time
        java.util.Date endTime = new SimpleDateFormat("HH:mm:ss")
                .parse(argEndTime);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endTime);

        //
        if (currentTime.compareTo(endTime) < 0) {

            currentCalendar.add(Calendar.DATE, 1);
            currentTime = currentCalendar.getTime();

        }

        if (startTime.compareTo(endTime) < 0) {

            startCalendar.add(Calendar.DATE, 1);
            startTime = startCalendar.getTime();

        }
        //
        if (currentTime.before(startTime)) {

            System.out.println(" Time is Lesser ");

            valid = false;
        } else {

            if (currentTime.after(endTime)) {
                endCalendar.add(Calendar.DATE, 1);
                endTime = endCalendar.getTime();

            }

            System.out.println("Comparing , Start Time /n " + startTime);
            System.out.println("Comparing , End Time /n " + endTime);
            System.out
                    .println("Comparing , Current Time /n " + currentTime);

            if (currentTime.before(endTime)) {
                System.out.println("RESULT, Time lies b/w");
                valid = true;
            } else {
                valid = false;
                System.out.println("RESULT, Time does not lies b/w");
            }

        }
        return valid;

    } else {
        throw new IllegalArgumentException(
                "Not a valid time, expecting HH:MM:SS format");
    }

}

所以现在我用秒00给出小时和分钟,然后用上面的方法传递它,但是我得到一个例外,即String或我输入的所有Strings是不是正确的格式。

我试了一下:

String start = "12:00:00";
String end = "15:00:00";
String current = "14:00:00";

它运作良好,但是当我以我需要的方式进入变量时,它不起作用并且异常被传递。

    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    int dayOfTheWeek = calendar.get(Calendar.DAY_OF_WEEK);
    int fromHour = biz.getOpeningHours().get(dayOfTheWeek-1).getFromHour();
    int fromMinute = biz.getOpeningHours().get(dayOfTheWeek-1).getFromMinute();
    int toHour = biz.getOpeningHours().get(dayOfTheWeek-1).getToHour();
    int toMinute = biz.getOpeningHours().get(dayOfTheWeek-1).getToMinute();

boolean insideTime = false;
    try {

        String start = ""+adjustText(fromHour)+":"+adjustText(fromMinute)+":00";
        String end = ""+adjustText(toHour)+":"+adjustText(toMinute)+":00";
        String current = ""+adjustText(hour)+":"+""+adjustText(minute)+":00";

        //String start = "12:00:00";
        //String end = "15:00:00";
        //String current = "14:00:00";
        insideTime = isTimeBetweenTwoTime(start,end,current);
    } catch (ParseException e) {
        e.printStackTrace();
    }

这是adjustText方法:

public String adjustText(int adjustTimeNumber){

    String a = "";
    if (adjustTimeNumber == 0) {
        a = ""+ adjustTimeNumber + "0";
    } else if (adjustTimeNumber > 0 && adjustTimeNumber < 10) {
        a = "0" + adjustTimeNumber;
    } else {
        a = adjustTimeNumber+"";
    }

    return a;

}

请帮助我没有得到它,strings是完美的但由于某种原因它不起作用。

1 个答案:

答案 0 :(得分:1)

我会避免将日期对象转换为字符串,然后将字符串解析回时间对象,感觉不需要。

试试这个:

// I assume your "time" object is called TimeObj, change it to the correct name in your implementation

Calendar now = Calendar.getInstance();

int dayOfTheWeek = calendar.get(Calendar.DAY_OF_WEEK);
TimeObj t = biz.getOpeningHours().get(dayOfTheWeek-1);

Calendar from = Calendar.getInstance();
from.set(Calendar.HOUR_OF_DAY, t.getFromHour());
from.set(Calender.MINUTE, t.getFromMinute());
from.set(Calender.SECOND, 0);

Calendar to = Calendar.getInstance();
to.set(Calendar.HOUR_OF_DAY, t.getToHour());
to.set(Calender.MINUTE, t.getToMinute());
to.set(Calender.SECOND, 0);

if (from.after(to)) {
    // this means to "to" is after midnight, add a day to "to"
    to.set(DATE, to.get(DATE) + 1);
}

boolean insideTime = now.after(from) && now.before(to);