DateFormat parse(String s)方法底层问题

纵骑横飞 2016-09-23 07:06:59
大家有没有注意到 SimpleDateFormate方法是没有parse(String s)方法的,如果要拆解指定的字符串,还得用DateFormat的parse方法,而传入的字符串模式是SimpleDateFormat指定的,那么DateFormat的parse(String s)方法是如何实现的呢?
...全文
601 11 打赏 收藏 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
不太明白问的什么意思,这是实现的源码

/**
     * Parses text from the beginning of the given string to produce a date.
     * The method may not use the entire text of the given string.
     * <p>
     * See the {@link #parse(String, ParsePosition)} method for more information
     * on date parsing.
     *
     * @param source A <code>String</code> whose beginning should be parsed.
     * @return A <code>Date</code> parsed from the string.
     * @exception ParseException if the beginning of the specified string
     *            cannot be parsed.
     */
    public Date parse(String source) throws ParseException
    {
        ParsePosition pos = new ParsePosition(0);
        Date result = parse(source, pos);
        if (pos.index == 0)
            throw new ParseException("Unparseable date: \"" + source + "\"" ,
                pos.errorIndex);
        return result;
    }
纵骑横飞 2016-09-23
  • 打赏
  • 举报
回复
人们怎么都忙着回答那些没营养的问题,要刷分去淘宝。
纵骑横飞 2016-09-23
  • 打赏
  • 举报
回复
引用 10 楼 leetao881212 的回复:
这个不是很正常的代码 父类DateFormat 处理具体框架层面的公用流程代码 SimpleDateFormat 负责具体的逻辑实现。 比如parse(string)方法,DateFormat 中调用抽象方法parse(string,ParsePosition), 该方法在SimpleDateFormat 中有具体定义,在里面用到了 pattern。
我傻逼
leetao881212 2016-09-23
  • 打赏
  • 举报
回复
这个不是很正常的代码 父类DateFormat 处理具体框架层面的公用流程代码 SimpleDateFormat 负责具体的逻辑实现。 比如parse(string)方法,DateFormat 中调用抽象方法parse(string,ParsePosition), 该方法在SimpleDateFormat 中有具体定义,在里面用到了 pattern。
家里敷泥呀 2016-09-23
  • 打赏
  • 举报
回复
引用 7 楼 fondOfJava 的回复:
我来看热闹的。。。。。。。。。
我只是吃瓜群众。
家里敷泥呀 2016-09-23
  • 打赏
  • 举报
回复
引用 6 楼 weixin_34881029 的回复:
[quote=引用 5 楼 Q80470101 的回复:] DateFormat 是 SimpleDateFormat 的父类, 子类可以直接使用父类的public方法。
那父类如何使用子类的匹配模式 不要轻率的回答[/quote] 你要这么果断的认为我是轻率的话,我只能回复“呵呵”了
fondOfJava 2016-09-23
  • 打赏
  • 举报
回复
我来看热闹的。。。。。。。。。
纵骑横飞 2016-09-23
  • 打赏
  • 举报
回复
引用 5 楼 Q80470101 的回复:
DateFormat 是 SimpleDateFormat 的父类, 子类可以直接使用父类的public方法。
那父类如何使用子类的匹配模式 不要轻率的回答
家里敷泥呀 2016-09-23
  • 打赏
  • 举报
回复
DateFormat 是 SimpleDateFormat 的父类, 子类可以直接使用父类的public方法。
  • 打赏
  • 举报
回复
建议楼主去看源代码
纵骑横飞 2016-09-23
  • 打赏
  • 举报
回复
引用 2 楼 qnmdcsdn 的回复:
不太明白问的什么意思,这是实现的源码

/**
     * Parses text from the beginning of the given string to produce a date.
     * The method may not use the entire text of the given string.
     * <p>
     * See the {@link #parse(String, ParsePosition)} method for more information
     * on date parsing.
     *
     * @param source A <code>String</code> whose beginning should be parsed.
     * @return A <code>Date</code> parsed from the string.
     * @exception ParseException if the beginning of the specified string
     *            cannot be parsed.
     */
    public Date parse(String source) throws ParseException
    {
        ParsePosition pos = new ParsePosition(0);
        Date result = parse(source, pos);
        if (pos.index == 0)
            throw new ParseException("Unparseable date: \"" + source + "\"" ,
                pos.errorIndex);
        return result;
    }
谢谢,我会看源代码 我的意思是parse(String s)是DateFormat提供的, 而你指定的模式是SimpleDateFormat里的,这个parse方法底层如何运作

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧