Advertisement
gurumutant

Validasi input tanggal

Oct 15th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  *
  8.  * @author hendri
  9.  */
  10. public class JavaApplication1 {
  11.  
  12.     /**
  13.      * @param args the command line arguments
  14.      */
  15.     public static void main(String[] args) {
  16.         Scanner in = new Scanner(System.in);
  17.         System.out.println("Ketikkan tanggal: ");
  18.         String tgl = in.next();
  19.         if (isDateValid(tgl))
  20.             System.out.println("Tanggal "+tgl+" valid !");
  21.         else
  22.             System.out.println("Tanggal "+tgl+" tidak valid !");
  23.     }
  24.    
  25.     public static boolean isDateValid(String date) {
  26.         try {
  27.             DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
  28.             df.setLenient(false);
  29.             df.parse(date);
  30.             return true;
  31.         } catch (ParseException e) {
  32.             return false;
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement