11

次に例を示します。

public MyDate() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
    sdf.setLenient(false);
    String t1 = "2011/12/12aaa";
    System.out.println(sdf.parse(t1));
}

2011/12/12aaa は有効な日付文字列ではありません。ただし、関数は「Mon Dec 12 00:00:00 PST 2011」を出力し、ParseException はスローされません。

SimpleDateFormat で "2011/12/12aaa" を無効な日付文字列として扱い、例外をスローする方法を誰か教えてもらえますか?

4

7 に答える 7

13

JavaDoc on にparse(...)は次のように記載されています。

解析では、必ずしも文字列の末尾までのすべての文字が使用されるとは限りません

SimpleDateFormat例外をスローすることはできないようですが、次のことができます。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
sdf.setLenient(false);
ParsePosition p = new ParsePosition( 0 );
String t1 = "2011/12/12aaa";    
System.out.println(sdf.parse(t1,p));

if(p.getIndex() < t1.length()) {
  throw new ParseException( t1, p.getIndex() );
}

基本的に、解析が文字列全体を消費したかどうかを確認し、そうでない場合は無効な入力があるかどうかを確認します。

于 2011-12-08T08:48:31.093 に答える
5

日付が有効かどうかを確認するには 次のメソッドは、日付が有効な場合に戻ります。そうでない場合は false を返します。

public boolean isValidDate(String date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
        Date testDate = null;
        try {
            testDate = sdf.parse(date);
        }
        catch (ParseException e) {
            return false;
        }
        if (!sdf.format(testDate).equals(date)) {
            return false;
        }
        return true;

    }

日付が有効かどうかを確認できる次のクラスを見てください

**サンプル例**

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateValidCheck {


    public static void main(String[] args) {

        if(new DateValidCheck().isValidDate("2011/12/12aaa")){
            System.out.println("...date is valid");
        }else{
            System.out.println("...date is invalid...");
        }

    }


    public boolean isValidDate(String date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d");
        Date testDate = null;
        try {
            testDate = sdf.parse(date);
        }
        catch (ParseException e) {
            return false;
        }
        if (!sdf.format(testDate).equals(date)) {
            return false;
        }
        return true;

    }

}
于 2011-12-08T09:46:29.747 に答える
2

パターン文字列全体が正常に解析された後、解析するSimpleDateFormatように指定されたデータの評価を停止します。

于 2011-12-08T09:03:28.620 に答える
0

ParsePositionクラスまたはsdf.setLenient(false)関数を使用できます

ドキュメント: http ://docs.oracle.com/javase/7/docs/api/java/text/ParsePosition.html http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat .html#setLenient(boolean)

于 2011-12-08T08:53:41.737 に答える
0

次のメソッドのドキュメントをご覧くださいParseException if the beginning of the specified string cannot be parsed

javadoc を含むメソッドのソース コード:

/**
 * 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;
}
于 2011-12-08T08:58:07.957 に答える