tags:

views:

79

answers:

1

I would like to display e.g. 31-12-1999 for years ac and 31-12-(-)2000 for years bc. I spent some time with the joda api's e.g. something like:

new DateTimeFormatterBuilder().appendYear(4, 10).appendLiteral("-").appendMonthOfYear(2).appendLiteral("-").appendDayOfMonth(2).toFormatter();

but I cannot find any details about this specific wish.

+1  A: 

For the AD case:

new DateTimeFormatterBuilder()
  .appendDayOfMonth(2).appendLiteral("-")
  .appendMonthOfYear(2).appendLiteral("-")
  .appendYear(4, 10).toFormatter();

For the BC case:

new DateTimeFormatterBuilder()
  .appendDayOfMonth(2).appendLiteral("-")
  .appendMonthOfYear(2).appendLiteral("-(-)")
  .appendYear(4, 10).toFormatter();

Then write an if statement before formatting.

If you want a single formatter to do this you can implement DateTimePrinter and then append that to DateTimeFormatterBuilder.

JodaStephen