我调用的是单个参数的方法,怎么我给它传"2008121a",我把这个转化成yyyy/mm/dd格式,我想应该报错,怎么输出的结果是2008/12/01???

解决方案 »

  1.   

    为什么我运行会报错啊?parse 转成的是一个 Date,怎么会输出呢?
      

  2.   

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date d = sdf.parse("2008121a");
    System.out.println(d);我这样就会报错啊,你是怎么弄的?
      

  3.   

    public class Test {    /**
         * @param args
         */
        public static void main(String[] args) {        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
            Date date = null;
            try {
                date = format.parse("2008/12/1a");
                System.out.println(date.toLocaleString());
            } catch (ParseException e) {
                // TODO 自動生成された catch ブロック
                e.printStackTrace();
                System.out.println("error");
            }
        }}
      

  4.   

    // TODO 自動生成された catch ブロック
    日文版本的  EClipse吧.....楼上的强人在强奸他钱掠来的新娘----这话是徐志摩说的,不是我
      

  5.   


    不管是你说的还是xxx说的我不介意!!!下一楼开始不想回答就别留言了,想找6楼这样的话题就谢绝了
      

  6.   

    toLocaleString已经不被推荐。 JDK 版本 1.1以后,
    被DateFormat.format(Date date)调换了。
      

  7.   

    为什么不抱错,只能看看源码了public Date parse(String text, ParsePosition pos)
    {
            int start = pos.index;
            int oldStart = start;
            int textLength = text.length();        boolean[] ambiguousYear = {false};        calendar.clear(); // Clears all the time fields        for (int i = 0; i < compiledPattern.length; ) {
                int tag = compiledPattern[i] >>> 8;
        int count = compiledPattern[i++] & 0xff;
        if (count == 255) {
    count = compiledPattern[i++] << 16;
    count |= compiledPattern[i++];
         }     switch (tag) {
        case TAG_QUOTE_ASCII_CHAR:
    if (start >= textLength || text.charAt(start) != (char)count) {
        pos.index = oldStart;
        pos.errorIndex = start;
        return null;
    }
    start++;
    break;     case TAG_QUOTE_CHARS:
    while (count-- > 0) {
        if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
    pos.index = oldStart;
    pos.errorIndex = start;
    return null;
        }
        start++;
    }
    break;     default:
    // Peek the next pattern to determine if we need to
    // obey the number of pattern letters for
    // parsing. It's required when parsing contiguous
    // digit text (e.g., "20010704") with a pattern which
    // has no delimiters between fields, like "yyyyMMdd".
    boolean obeyCount = false;
    if (i < compiledPattern.length) {
        int nextTag = compiledPattern[i] >>> 8;
        if (!(nextTag == TAG_QUOTE_ASCII_CHAR || nextTag == TAG_QUOTE_CHARS)) {
    obeyCount = true;
        }
    }
    start = subParse(text, start, tag, count, obeyCount,
     ambiguousYear, pos);
    if (start < 0) {
        pos.index = oldStart;
        return null;
    }
        }
    } // If only AM_PM has been set without any hour value, then
    // HOUR is set to 0 to force calendar to take the HOUR and
    // AM_PM fields. (bugid: 4736959) (Changing GregorianCalendar
    // to take a look at both the HOUR and AM_PM stamp values
    // breaks JCK GregorianCalendar2057.)
    if (calendar.isSet(Calendar.AM_PM) && !calendar.isSet(Calendar.HOUR) &&
        !calendar.isSet(Calendar.HOUR_OF_DAY)) {
        calendar.set(Calendar.HOUR, 0); // force to update the stamp value
    }        // At this point the fields of Calendar have been set.  Calendar
            // will fill in default values for missing fields when the time
            // is computed.        pos.index = start;        // This part is a problem:  When we call parsedDate.after, we compute the time.
            // Take the date April 3 2004 at 2:30 am.  When this is first set up, the year
            // will be wrong if we're parsing a 2-digit year pattern.  It will be 1904.
            // April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day.  2:30 am
            // is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
            // on that day.  It is therefore parsed out to fields as 3:30 am.  Then we
            // add 100 years, and get April 3 2004 at 3:30 am.  Note that April 3 2004 is
            // a Saturday, so it can have a 2:30 am -- and it should. [LIU]
            /*
            Date parsedDate = calendar.getTime();
            if( ambiguousYear[0] && !parsedDate.after(defaultCenturyStart) ) {
                calendar.add(Calendar.YEAR, 100);
                parsedDate = calendar.getTime();
            }
            */
            // Because of the above condition, save off the fields in case we need to readjust.
            // The procedure we use here is not particularly efficient, but there is no other
            // way to do this given the API restrictions present in Calendar.  We minimize
            // inefficiency by only performing this computation when it might apply, that is,
            // when the two-digit year is equal to the start year, and thus might fall at the
            // front or the back of the default century.  This only works because we adjust
            // the year correctly to start with in other cases -- see subParse().
            Date parsedDate;
            try {
                if (ambiguousYear[0]) // If this is true then the two-digit year == the default start year
                {
                    // We need a copy of the fields, and we need to avoid triggering a call to
                    // complete(), which will recalculate the fields.  Since we can't access
                    // the fields[] array in Calendar, we clone the entire object.  This will
                    // stop working if Calendar.clone() is ever rewritten to call complete().
                    Calendar savedCalendar = (Calendar)calendar.clone();
                    parsedDate = calendar.getTime();
                    if (parsedDate.before(defaultCenturyStart))
                    {
                        // We can't use add here because that does a complete() first.
                        savedCalendar.set(Calendar.YEAR, defaultCenturyStartYear + 100);
                        parsedDate = savedCalendar.getTime();
                    }
                }
                else parsedDate = calendar.getTime();
            }
            // An IllegalArgumentException will be thrown by Calendar.getTime()
            // if any fields are out of range, e.g., MONTH == 17.
            catch (IllegalArgumentException e) {
                pos.errorIndex = start;
                pos.index = oldStart;
                return null;
            }        return parsedDate;
        }
      

  8.   


    public class Test {    /**
         * @param args
         */
        public static void main(String[] args) {        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
            Date date = null;
            try {
                date = format.parse("2008/12/1a");
                //改成date = format.parse("2008121a");就报错了.
                System.out.println(date.toLocaleString());
            } catch (ParseException e) {
                // TODO 自動生成された catch ブロック
                e.printStackTrace();
                System.out.println("error");
            }
        }}
      

  9.   

    偶尔说了点恶心东西就给鄙视了
    好吧,我回点像样的吧
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date d = sdf.parse("2008121");
    System.out.println(d);
    也会报错的
    为什么,你从一开始就没有满足你自己定义的pattern

    Date d = sdf.parse("2008-12=-1a");之所以能够通过,并得到结果是因为在两个/d+[-]/d+-/d+ 以后末尾的非数字的字符被屏弃掉了
    这样你就能满足来完成,当然了,你也可以认为这是一个不对的,但是JAVA就是这样去做,来提高format的匹配能力的
    好了,就是这样了....
    还有被人鄙视真的很不爽撒!!!