Monthly Archives: October 2011

Java SimpleDateFormat Example

I keep forgetting the exact syntax of the SimpleDateFormat. The below example passes in a month value say “02” and returns “February”.
However this can work to transfer any date format into another format.

Refer to the Java 7 doc for the Date and Time Patterns


import java.text.SimpleDateFormat;

public String formatMonth(String month) {
SimpleDateFormat monthParse = new SimpleDateFormat("MM");
SimpleDateFormat monthDisplay = new SimpleDateFormat("MMMM");
return monthDisplay.format(monthParse.parse(month));
}

formatMonth("2");
Result = February