powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / Контроль преобразования String в Дату
2 сообщений из 2, страница 1 из 1
Контроль преобразования String в Дату
    #32744079
Nzk
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Nzk
Гость
Столкнулся с ситуацией, пременная 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
Подскажите начинающему, как такого избежать, или каким способом пользоваться при преобразовании, что-бы получить хотя бы ошибку.
Спасибо.
...
Рейтинг: 0 / 0
Контроль преобразования String в Дату
    #32744472
NotGonnaGetUs
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
/**
* 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).
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / Контроль преобразования String в Дату
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]