Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Strings
Home > Programs > Strings

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

Answer:

<p>import java.text.DateFormat;<br />
import java.text.ParseException;<br />
import java.text.SimpleDateFormat;<br />
import java.util.Scanner;</p>

<p>class DateValid {</p>

<p>    public static void main(String[] args) {</p>

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

<p>    private static boolean isDateValid(String date) {</p>

<p>        boolean result = false;</p>

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

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

<p>    }</p>

<p>}</p>

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here: