Convert Date to String using Apache Commons

This code example shows how to convert a Date object to String using the Apache Commons Beanutils library.

The Beanutils library have a converter called DateTimeConverter with which we can convert a Date object to a String object. The DateTimeConverter is an abstract class, so we need to subclass it and override the method getDefaultType. In our class we then implement a method called doConversion in which we call setPattern (inherited from DateTimeConverter) and specify the pattern we want the String to be displayed in. The pattern format follows standard Java patterns for date and time. Finally we just call the convert method (also inherited from superclass DateTimeConverter) passing in the String class and the Date to convert from.

The output from the code looks like this:

Search for more Java info on this site here:
Custom Search

Please type any questions here.