Advertisement
Garro

Java - Validar ingreso de fecha

Dec 7th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.text.SimpleDateFormat;
  2. import java.util.Scanner;
  3. import java.util.Date;
  4. import java.util.Calendar;
  5. import java.util.InputMismatchException;
  6. import java.lang.IllegalArgumentException;
  7.  
  8. public class Fecha{
  9.    public static void main(String[]args){
  10.       Date birthday = askForDate();
  11.       System.out.println(birthday.toString());
  12.    }
  13.  
  14.    public static Date askForDate(){
  15.       Scanner input_reader = new Scanner(System.in);
  16.       while(true){
  17.          try{
  18.             System.out.print("Ingrese día: ");
  19.             int input_day = input_reader.nextInt();
  20.  
  21.             System.out.print("Ingrese mes (número): ");
  22.             int input_month = input_reader.nextInt();
  23.  
  24.             System.out.print("Ingrese año: ");
  25.             int input_year = input_reader.nextInt();
  26.            
  27.             Calendar time = Calendar.getInstance();
  28.             time.setLenient(false);
  29.             time.clear();
  30.             time.set(input_year,input_month-1,input_day);
  31.  
  32.             return time.getTime();
  33.  
  34.          }catch(InputMismatchException e){
  35.             System.out.println("ERROR: Valor ingresado no es número.");
  36.             input_reader.nextLine();
  37.          }catch(java.lang.IllegalArgumentException e){
  38.             System.out.println("ERROR: Fecha ingresada no es valida.");
  39.          }
  40.       }
  41.    }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement