HOWTO format dates using POJava DateTime

The DateTime class is used for parsing, storing, and manipulating Date and Time. It also incorporates the ability to format dates in its toString output in a couple of ways.

toString("custom_format")

By default, DateTime will use the format defined in its associated DateTimeConfig. You can override this format by specifying a custom format as a parameter. Here's an example:

DateTime dt=new DateTime("2011-10-05");
system.out.println(dt.toString("yyyy-MM-dd"));

Format specifiers

The formatting options behave the same as the SimpleDateFormat provided in the Java language, with a few exceptions.

Format characterJava behaviorPOJava behavior
gUnsupportedScientific Era Designator CE/BCE
GEra DesignatorSame
MMonth in YearSame
yYearSame
dDay in MonthSame
DDay in YearSame
EDay in WeekSame
aAm/Pm MarkerSame
hHour in am/pm 1-12Same
HHour in Day 0-23Same
kHour in Day 1-24Same
KHour in am/pm 0-11Same
mMinute in HourSame
sSecond in MinuteSame
SMillisecondFractional Second
zGeneral Time ZoneSame
ZRFC 822 Time ZoneSame
FDay of Week in MonthSame
wWeek in YearSame
WWeek in MonthSame