Thursday, 23 May 2013

JAXB with an adapter: Date formatting example

So, moving along from the absolute basics, I will now demonstrate how to add an adapter for formatting.

We had a kennel class which contained a list of puppies.  Each puppy has a birthday and a timestamp when it arrived in the kennel.  These are both of the type XMLGregorianCalendar but I would rather have use Joda LocalDate and LocalDateTime. Therefore I add an adapter which is called when the contents of the <birthday> and <timestamp> tags are unmarshalled.

The DateAdapter.java has two static methods, unmarshal and marshal, which convert a string to a LocalDate and vice versa respectively.


Run it to generate the code:

xjc ../resources/puppy/kennel.xsd -p com.ojha.jaxb.kennel.generated


The DateAdapter generates an Adapter1.java

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning...