Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SimpleDataFormat分析错误位置_Java_Simpledateformat_Parseexception - Fatal编程技术网

Java SimpleDataFormat分析错误位置

Java SimpleDataFormat分析错误位置,java,simpledateformat,parseexception,Java,Simpledateformat,Parseexception,如果用户输入的日期超出范围(例如month=22),我想通知用户。但dateFormat存储在全局设置文件中,所以我不知道输入字符串中月份字段的确切位置。我尝试使用ParseException的getErrorOffset()方法,但它总是返回输入字符串的结束位置(10) SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); sdf.setLenient(false); try { s

如果用户输入的日期超出范围(例如month=22),我想通知用户。但dateFormat存储在全局设置文件中,所以我不知道输入字符串中月份字段的确切位置。我尝试使用ParseException的getErrorOffset()方法,但它总是返回输入字符串的结束位置(10)

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    sdf.setLenient(false);
    try
    {
        sdf.parse("22/12/2009");
    }
    catch (ParseException ex)
    {
        ex.getErrorOffset();
    }
有什么解决办法吗?
thx

您想关闭宽松解析:

sdf.setLenient(false);

请参见

要关闭宽松解析:

sdf.setLenient(false);
请参见

请参见:

如果发生错误,则不会更改pos的索引,pos的错误索引将设置为发生错误的字符的索引,并返回null

见:

如果发生错误,则不会更改pos的索引,pos的错误索引将设置为发生错误的字符的索引,并返回null


我想告诉用户他在哪个日期犯了错误。并将异常“不可解析日期:”22/12/2009“替换为“月数应在1和12之间”。我想告诉用户他在哪个日期犯了错误。并替换异常“不可解析日期:”22/12/2009”“到”月数应在1和12之间“`SimpleDateFormat sdf=新SimpleDateFormat(“MM/dd/yyyy”,Locale.US);sdf.setLenient(错误);ParsePosition pos=新的ParsePosition(0);sdf.parse(“22/12/2012”,pos);`此代码在index和errorindex中都返回10`SimpleDateFormat sdf=newsimpledateformat(“MM/dd/yyyy”,Locale.US);sdf.setLenient(错误);ParsePosition pos=新的ParsePosition(0);sdf.parse(“22/12/2012”,pos);`此代码在index和errorindex中都返回10