|
|
|
Контроль преобразования String в Дату
|
|||
|---|---|---|---|
|
#18+
Столкнулся с ситуацией, пременная aS имеет значения '30.30.2004' Следующий код ошибки не выдал SimpleDateFormat df = new SimpleDateFormat ("dd.MM.yyyy"); Date vToDate; try{ vToDate = df.parse(aS); System.out.println(vToDate); } catch (Exception e) { vToDate = null; System.out.println("Ошибка при преобразовании даты !!! "); } и в рекзультате я получил дату 30.06.2006 :-O Подскажите начинающему, как такого избежать, или каким способом пользоваться при преобразовании, что-бы получить хотя бы ошибку. Спасибо. ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 19.10.2004, 11:41 |
|
||
|
Контроль преобразования String в Дату
|
|||
|---|---|---|---|
|
#18+
/** * Parses text from a string to produce a <code>Date</code>. * <p> * The method attempts to parse text starting at the index given by * <code>pos</code>. * If parsing succeeds, then the index of <code>pos</code> is updated * to the index after the last character used (parsing does not necessarily * use all characters up to the end of the string), and the parsed * date is returned. The updated <code>pos</code> can be used to * indicate the starting point for the next call to this method. * If an error occurs, then the index of <code>pos</code> is not * changed, the error index of <code>pos</code> is set to the index of * the character where the error occurred, and null is returned. * * @param text A <code>String</code>, part of which should be parsed. * @param pos A <code>ParsePosition</code> object with index and error * index information as described above. * @return A <code>Date</code> parsed from the string. In case of * error, returns null. * @exception NullPointerException if <code>text</code> or <code>pos</code> is null. */ public Date parse(String text, ParsePosition pos); /** * 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; } ^^^ JavaDocs В твоём случае pos.index = 4 или около того. поэтому ошибка не генериться. Пользуйся методом SimpleDateFormat#parse(String, ParsePosition). ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 19.10.2004, 14:09 |
|
||
|
|

start [/forum/topic.php?fid=59&gotonew=1&tid=2153576]: |
0ms |
get settings: |
8ms |
get forum list: |
9ms |
check forum access: |
2ms |
check topic access: |
2ms |
track hit: |
48ms |
get topic data: |
8ms |
get first new msg: |
4ms |
get forum data: |
1ms |
get page messages: |
35ms |
get tp. blocked users: |
1ms |
| others: | 191ms |
| total: | 309ms |

| 0 / 0 |
