首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SimpleDateFormat:parse()被跳过

SimpleDateFormat:parse()被跳过
EN

Stack Overflow用户
提问于 2015-12-07 22:25:56
回答 1查看 72关注 0票数 1

我对Java/Android开发非常陌生。我正在尝试编写一个简单的Android应用程序,作为其中的一部分,我需要将日期从字符串转换到日期。

我有以下方法:

代码语言:javascript
运行
复制
private Date convertFromString(String birthdate) {
        String regex = "^(?:(?:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$\n";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(birthdate);
        Date date = null;
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.UK);

        if (matcher.matches()) {
            try {
                Calendar cal = Calendar.getInstance(); // <-- this,
                cal.setTime(format.parse(birthdate)); // and that line gets skipped by debugger step
                System.out.print(cal); // this line gets executed
            } catch (ParseException exception) {
                System.out.print("wtf??");
            }
        }
        return date;
    }

不管传递给方法的字符串值如何,它总是返回null。当我跨过上面标有调试器行的代码时,就会被调试器跳过,它不会让我介入,就好像format.parse(..)从来没有被调用过一样?

方法中有一些调试代码是有意的

在方法调用期间没有异常抛出,我传递有效的数据!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-07 22:39:23

( 1)你根本没有填写日期:

代码语言:javascript
运行
复制
Calendar cal = Calendar.getInstance(); // <-- this,
                cal.setTime(format.parse(birthdate)); // and that line gets skipped by debugger step
                System.out.print(cal);

你设定了卡尔,但没有确定日期

2)我用"24/11/1980“调用这个方法,matcher.matches()返回false,它在if(matcher.matches())中看起来是个问题,但是调试器显示了错误的行。在我将"if(matcher.matches())“更改为"if(true)”后,此方法将打印matcher.matches.。你为什么不使用:

代码语言:javascript
运行
复制
      private Date convertFromString(String birthdate) {
            Date date = null;
           SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.UK);

            try {
                Calendar cal = Calendar.getInstance(); // <-- this,
                cal.setTime(format.parse(birthdate)); // and that line gets skipped by debugger step
                System.out.print(cal); // this line gets executed
                return cal.getTime();   
            } catch (ParseException exception) {
                System.out.print("wtf??");
            }
        return null;
   }

如果您需要进行一些验证,例如,它可以对reg模式的cal insead进行验证:

代码语言:javascript
运行
复制
             cal.before(new Date());
             Calendar beforeHundreadYears = Calendar.getInstance();
             beforeHundreadYears.set(1915, 0, 0);
             cal.after(beforeHundreadYears);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34144468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档