Date Validation

Hi all, hari ini masih tentang Java. Kata jeng Yoan, udah nyampe materi “kromo inggil” hehehe…Mid testnya parah abis, soalnya buanyak, waktunya mepet. Gimana ga hectic, mana mo presentasi lagi, huhuhu. Tapi alhamdulillah, presentasinya mayan lancar, meski nervous abisss!!! Eiya, pas presentasi kemaren make laser pen yg kemaren nitip ke Edu (thanks again, Du!), trus anak2 tu pada “Waaaaa!!!” hehehe…lebay yak.

Oke, kembali ke laptop (*halah). Berhubung masih cemen ttg Java (blajar pas kul doang, trus ga pernah dipake lagi), jadi bingung abis pas mo validasi field yg inputnya tanggal. Beruntunglah ada gugel. Nemu ni satu method buat validasinya, tinggal copy-paste trus edit dikit biar bisa cek kalo user masukin huruf ato karakter lain ada warningnya juga. Jangan lupa import dulu java.util.StringTokenizer-nya.

    public static boolean testDate(String str) {
        int Jan = 31;
        int Mar = 31;
        int Apr = 30;
        int May = 31;
        int Jun = 30;
        int Jul = 31;
        int Aug = 31;
        int Sep = 30;
        int Oct = 31;
        int Nov = 30;
        int Dec = 31;
        int day=1;
        int month=1;
        int year=1900;
       
        boolean flag = true;
        StringTokenizer st = new StringTokenizer(str, “/”);

        try {
         day = Integer.parseInt(st.nextToken());
            month = Integer.parseInt(st.nextToken());
            year = Integer.parseInt(st.nextToken());
        } catch(Exception ex) {
         JOptionPane.showMessageDialog(null, “Invalid character in date field!” , “alert”, JOptionPane.WARNING_MESSAGE);
         return false;
        }
       
        if ((month > 12) && (month < 100)) {
            flag = false;
        }

        switch (month) {
        case 1:
            if (day > Jan) {
                flag = false;
            }
            break;

        case 2:
            if ((year % 4) == 0) {
                if (day > 29) {
                    flag = false;
                }
            } else {
                if (day > 28) {
                    flag = false;
                }
            }

        case 3:
            if (day > Mar) {
                flag = false;
            }
            break;

        case 4:
            if (day > Apr) {
                flag = false;
            }
            break;

        case 5:
            if (day > May) {
                flag = false;
            }

            break;

        case 6:
            if (day > Jun) {
                flag = false;
            }
            break;

        case 7:
            if (day > Jul) {
                flag = false;
            }
            break;

        case 8:
            if (day > Aug) {
                flag = false;
            }
            break;

        case 9:
            if (day > Sep) {
                flag = false;
            }
            break;

        case 10:
            if (day > Oct) {
                flag = false;
            }
            break;

        case 11:
            if (day > Nov) {
                flag = false;
            }
            break;

        case 12:
            if (day > Dec) {
                flag = false;
            }
            break;
        }

        return flag;
    }

Emang sih rada panjang methodnya, ada yg punya cara lain yg lebih efektif ga?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s