Java defines a class (java.text.SimpleDateFormat) that provides a convenient way to convert strings with date and/or time information so that it appears in your desired format.

Date and time patterns

Date and time formats are specified with date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted with single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they are simply copied into the output string during formatting or matched against the input string during parsing.

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):


For this example, consider the date and time as 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone. The following examples show how date and time patterns are interpreted in the U.S. locale:

You can test out a date and time pattern using this online tool.

Additional references

JavaDoc for java.text.SimpleDateFormat class