Take FREE!! Online Mettl Mock Test to Crack TechM and Other Companies Written Exams.
Get Off-Campus Placement Jobs Info !!!
TCS NQT Technical and Managerial Round Interview Questions

Program Discussion :: Strings

Home > Programs > Strings

35 / 59

Write a program to check whether given date is valid or not?

Answer:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

class DateValid {

   public static void main(String[] args) {

      Scanner input = null;
      try {
         input = new Scanner(System.in);
         System.out.println("Enter date in dd/MM/yyyy format : ");
         String date = input.next();
         if (isDateValid(date)) {
            System.out.println("Date is valid.");
         } else {
            System.out.println("Date is not valid.");
         }
      } finally {
         if (input != null) {
            input.close();
         }
      }
   }

   private static boolean isDateValid(String date) {

      boolean result = false;

      if (isValidDateFormat(date)) {
         DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
         dateFormat.setLenient(false);
         try {
            dateFormat.parse(date);
            result = true;
         } catch (ParseException e) {
            result = false;
         }
      }
      return result;
   }

   private static boolean isValidDateFormat(String date) {
      String pattern = "(0?[1-9]|[12][0-9]|3[01])\\/(0?[1-9]|1[0-2])\\/([0-9]{4})";
      boolean result = false;
      if (date.matches(pattern)) {
         result = true;
      }
      return result;

   }

}

Asked In :: Virtusa

Post Your Answer Here:

Language:

Post Your Reply Here: