HƯỚNG DẪN Check isInteger or isDate

Joe

Thành viên VIP
21/1/13
3,022
1,335
113
Thanks a ton for your posting =D>. I've verified only the 2 methods isDate and isInteger. Both work fine, but a little excessive.
  1. isInteger: the string s should be ckecked for PLUS sign and null (otherwise NullPointerException could occur).
  2. isDate: String s should be check for null, too. And the local variables formatter and converseDate can be omitted and direct used.
The optimal codes could be as following:
Java:
import java.text.SimpleDateFormat;

public class Test {
    public static boolean isInteger(String s) {
        return isInteger(s, 10);
    }

    public static boolean isInteger(String s, int radix) {
        if (s == null || s.isEmpty()) return false;
        int l = s.length(), i = 0;
        char c = s.char(0);
        if (c == '-' || c == '+')  {
            if (l == 1) return false;
            ++i;
         }
        for (; i < l; i++) {
            if (Character.digit(s.charAt(i), radix) < 0) return false;
        }
        return true;
    }

    public static boolean isDate(String s) {
      /*
        // Instantiating variables

        DateFormat formatter = null;
        Date convertDate = null;

        // Use one of the following date formats.

        // You can also add your own regex (Regular Expression)
        if (s.matches("([0-9]{4})")) {
            formatter = new SimpleDateFormat("yyyy");
        } else if (s.matches("([0-9]{2})/([0-9]{4})")) {
            formatter = new SimpleDateFormat("MM/yyyy");
        } else if (s.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})")) {
            formatter = new SimpleDateFormat("dd/MM/yyyy");
        }

        try {
            convertDate = formatter.parse(s);
            return true;

        } catch (Exception ex) { // Catch the Exception in case the format is not found.

        }
        return false;
      */
        String fs = null;
        // Use one of the following date formats.

        // You can also add your own regex (Regular Expression)
        if (s == null) return false;
        else if (s.matches("([0-9]{4})")) {
            fs = "yyyy";
        } else if (s.matches("([0-9]{2})/([0-9]{4})")) {
            fs = "MM/yyyy";
        } else if (s.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})")) {
            fs = "dd/MM/yyyy";
        }

        try {
            (new SimpleDateFormat(fs)).parse(s);
            return true;

        } catch (Exception ex) { // Catch the Exception in case the format is not found.

        }
        return false;
    }

    public static void main(String args[]) {
    String s1 = "15/10/2021";
    String s2 = "2222222222";
    String s3 = "1sar4";
   System.out.println("Is Date?\ns1:"+Test.isDate(s1)+"\ns2:"+Test.isDate(s2)+"\ns3:"+Test.isDate(s3)+
      "\n\nIs Integer?\ns1:"+Test.isInteger(s1)+"\ns2:"+Test.isInteger(s2)+"\ns3:"+Test.isInteger(s3));
  }
}
and the test's result:
Code:
C:\links\java\memberTools>java Test
Is Date?
s1:true
s2:false
s3:false

Is Integer?
s1:false
s2:true
s3:false

C:\links\java\memberTools>
 
Sửa lần cuối: