Wednesday, December 16, 2009

JODA Time

I was looking for an efficient date time processing in java, as I believe that there is a bit of a hitch in java's date time design (the Calendar class..) as it lacks some of the simple and basic methods for the date processing which is developer friendly.

Thanks to the project Joda Time (led by Stephen Colebourne) which is a developer friendly alternative to the JAVA's date and time API.
Here is a very basic example using the Joda time and the Calendar API.

To calculate the current year using the Calendar API :

Calendar today = Calendar.getInstance();
int year = today.get( Calendar.YEAR );

But in Joda time :

DateTime today = new DateTime();
int year = today.getYear();

For more refer Joda.org ......

Thursday, December 10, 2009

Exception handling

An intelligent and effective coding includes "Exception handling". Java includes three types of exceptions checked exceptions, un-checked exceptions & error.

Checked exceptions : Exceptions which are sub class of the java.lang.Exception (eg: IOExceptions)
Un-checked exceptions : Exceptions which are sub class of the java.lang.RuntimeException (eg: NullPointerException)
Error : Exceptions which are sub class of the java.lang.Error (eg: AssertionError)

In an application only three styles of exception handling can be implemented
  1. Do not handle any exceptions.
  2. Handle the exceptions in a haphazard way.
  3. Handle the exception in an intelligent way.
Though one can easily conclude in an application the exception is handled or not but it is left to the reader to decide whether it is haphazard or intelligent way of handling the exceptions. You can find one of the good in detailed example on the exception handling here. Also, check this "An Exception Handling Framework for J2EE Applications".