Simple Date Format Exception
Simple Date Format Exception inherits from a package name java.text.SimpleDateFormat and implements interfaceCloneable,Serializable.SimpleDateFormat is a concrete class. The class is used for formatting and parsing the dates in local-sensitive matter. This allows you for formatting, parse and normalization of date. Simple Date-format provides you to work on choosing any user-defined patterns for date-time format. You can create a date-time formatter with get Time Instance,getDateInstance.These Class method returns a date-time formatter initialized with a specified given format.
Format of Dates and Time Pattern
These Date and time formats are reconogised by date and time pattern strings. The pattern of date and time string from 'A-Z' and from 'a-z' are presented as pattern letters includes the component of a date and time string. Text in date format can be represented in single quotes.
Let Understand Some Pattern in Date and Time Format
1.Text-If the number of pattern is more than 4 or more, the abbreviation is used. Otherwise a short form is used for it.
2.Number-The Number of pattern letter is the minimum number of digits, and shorter number.
3.Year-In case of year formatting, if the number of pattern 2 or more than this, the year is truncated., else it is interpreted as a number.
for example-if you write "10/10/11" then year format of "10/10/11" is parses to Oct 10,11AD.
4.Month- The number of pattern letter is 3 or more than that, then month is reconogized as a text. Otherwise it is considered as number.
1)SimpleDateFormat( )-creates a SimpleDateFormat using the date format symbol for the default locale.
2)SimpleDateFormat(String pattern)-creates a SimpleDateFormat using the specified pattern and date format symbols for the default locale.
3)Simple Date Format (String pattern,DateFormatSymbol)-creates
List of method used in Date Time Format
Let us review through constructor used in date
1) Date( )-This Constructor initializes an Object with the current date and time.
2)Date(long ms)-This Constructor accepts an argument, represent the millisecond that have elapsed since 1st January 1970.The date is account as reference date used for calculating the total number of millisecond that have passed till from current date.
Method used in Date Class are
Let Us Understand with Simple Example
In this program Example we want to describe a code that help you in displaying the current date ,month ,year and time and also calculate the total number of time in millisecond that elapsed since 1970.For this we define a class name DateCount.Inside this class we have a main method, in which we assign the memory to the instance of Date class. The System.out.println is used to accept and display the current date,month,year and time.
Date( )-Initializes an object with current time and date.
getTime( )-Provides you the millisecond that have elapsed since 1st January 1970.
Output on Command Prompt
Understand Simple Date Format Exception Example
In this program code we want to describe you the date,month,year,hour,minute and second set by you in date String variable, this variable is send as argument to the object of date. If the date object d is null the output will display an error showing parse returned null, else the output will display the argument set by you in string. But on Compilation the program will show you an exception.
Output On Command Prompt
How to Overcome with above Exception
In this code of the program we want to describe you how to overcome the exception, we check the line of the code where the exception occur .The line in which the exception occurs should be placed in the try block, followed by subsequent catch block that is used to handle the exception occur in the try block. The code contain an exception in parse( ) method, so we place the given method in try block.
Output on Command Prompt