Johntk Johntk - 5 months ago 52
Java Question

Joda-Time: convert SimpleDateFormat pattern to DateTimeFormat.forPattern

I'm trying to modify some code to use

rather than

Currently the code is using
and SimpleDateFormat:

public static final ThreadLocal<DateFormat> FORMAT_TIMESTAMP = new ThreadLocal<DateFormat>() {
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

public static String format(Timestamp timestamp) {
return FORMAT_TIMESTAMP.get().format(timestamp);

My understanding is that Joda-time is thread safe, so there is no need to use

With this in mind I have modified the code to this:

public static String format(Instant timestamp) {

Instant formated = Instant.parse(timestamp.toString(), DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssXXX"));

return formated.toString();

If I nned to insert the values into a DB later in the code I plan to use this method.

Assuming I'm going about this the right way, is there anyway to format the
like the


At the moment I'm getting a
Invalid format


X is not recognised by Joda. Replacing the XXX by ZZ should do what you need.

Because DateTimeFormat is thread safe, you can share it across threads. So your code could look like this:

private static final DateTimeFormatter FORMAT_TIMESTAMP =

public static String format(Instant timestamp) {
    return FORMAT_TIMESTAMP.print(timestamp);