gurumutant icon

Validasi input tanggal

gurumutant | PRO | 10/15/18 10:46:37 PM UTC | 0 ⭐ | 631 👁️ | Never ⏰ | []
Java |

910 B

|

None

|

0 👍

/

0 👎

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
 
/**
 *
 * @author hendri
 */
public class JavaApplication1 {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Ketikkan tanggal: ");
        String tgl = in.next();
        if (isDateValid(tgl)) 
            System.out.println("Tanggal "+tgl+" valid !");
        else
            System.out.println("Tanggal "+tgl+" tidak valid !");
    }
    
    public static boolean isDateValid(String date) {
        try {
            DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
            df.setLenient(false);
            df.parse(date);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }
}

Comments