[an error occurred while processing this directive]

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.

 Constructor Syntax

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 a SimpleDateFormat using the given pattern and date format symbols.

List of method used in Date Time Format

                              Method                          Description
             void applyLocalizedPattern(String pattern) Provides you to apply the localized pattern string to date format
             void apply pattern(String pattern) Provides you to applies the pattern string  to date format
             boolean equals(Object obj) Provides you to compare the object with simpledateformat for equality

DateFormatSymbols get DateFormatSymbols

Provides you the copy value of the date  format

Date parse string text,parseposition pos)

Provides  you the parses text from a string to produce Date

 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

Method

Description

        long getime( ) Provides you the total count of millisecond that have passed since 1st January 1970
       void setTime(long time) Provides you the time and date that is specified by the time parameter.

 

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.

         import java.util.Date;
         class DateCount
{
         public static void main(String args[])
{
         Date date = new Date( );       
         System.out.println("The current date and time are:" +date);
         long ms=date.getTime( );
         System.out.println("The number of milliseconds elasped since Jan 1 1970 are" +ms);
}

};

 Output on Command Prompt


C:\>cd saurabh\

C:\saurabh>javac DateClass.java

C:\saurabh>java DateClass
The current date and time are:Wed Oct 22 03:29:20 GMT+05:30 2008
The number of milliseconds elasped since Jan 1 1970 are1224626360562

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.

          import java.util.Date;
          import java.check.SimpleDateFormat;

          public class CheckSimpleDateFormat
{
          public static void main (String[] args)
{
        SimpleDateFormat dateParser = new SimpleDateFormat("MM'/'dd'/'yyyy' 'H':'m':'s'.'SSS");
         String dateString = "05/18/2008 16:30:53.700";
         Date d = dateParser.parse(dateString);
         if( d == null )
{
        System.err.println("ERROR: parse returned null");
}
        else
{
       System.out.println("Date parsed: value = " + d );
}
}
}

Output On Command Prompt

C\saurabh>javac CheckSimpleDateFormat.java
stSimpleDateFormat.java:14: unreported exception java.text.ParseException; must be caught or declared to be throw

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.

                 import java.util.Date;
                 import java.text.SimpleDateFormat;

               public class CheckSimpleDateFormat
{
               public static void main (String[] args)
{
              SimpleDateFormat dateParser = new SimpleDateFormat("MM'/'dd'/'yyyy'   'H':'m':'s'.'SSS");
               try
{
              String dateString = "05/18/2008 16:30:53.700";
              Date   e             = dateParser.parse(dateString);
              if( e == null )
             System.err.println("ERROR: parse returned null");
             else
             System.out.println("Date parsed: value = " + e );
}
             catch( Exception e )
{
             System.err.println ("ERROR: this date parser threw an exception:\n " +   dateParser.toString() );
             if (e instanceof java.text.ParseException)
            System.err.println(" Index = " + ((java.text.ParseException)e).getErrorOffset ( ));
            e.printStackTrace (System.err);
}
}
}

Output on Command Prompt

C:\saurabh>javac CheckSimpleDateFormat.java

C:\saurabh>java CheckSimpleDateFormat
Date parsed: value = Sun May 18 16:30:53 GMT+05:30 2008

                         

[an error occurred while processing this directive]