Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.SimpleDateFormat;
- import java.util.Scanner;
- import java.util.Date;
- import java.util.Calendar;
- import java.util.InputMismatchException;
- import java.lang.IllegalArgumentException;
- public class Fecha{
- public static void main(String[]args){
- Date birthday = askForDate();
- System.out.println(birthday.toString());
- }
- public static Date askForDate(){
- Scanner input_reader = new Scanner(System.in);
- while(true){
- try{
- System.out.print("Ingrese día: ");
- int input_day = input_reader.nextInt();
- System.out.print("Ingrese mes (número): ");
- int input_month = input_reader.nextInt();
- System.out.print("Ingrese año: ");
- int input_year = input_reader.nextInt();
- Calendar time = Calendar.getInstance();
- time.setLenient(false);
- time.clear();
- time.set(input_year,input_month-1,input_day);
- return time.getTime();
- }catch(InputMismatchException e){
- System.out.println("ERROR: Valor ingresado no es número.");
- input_reader.nextLine();
- }catch(java.lang.IllegalArgumentException e){
- System.out.println("ERROR: Fecha ingresada no es valida.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement