You can make this conversion using the constructors provided by
java.text.SimpleDateFormat class and some patterns, such as
dd-MM-yy and so on, so as to format the
Date as you wish. We will show more examples of patterns and format symbols in the following sections.
1. Java SimpleDateFormat – Constructors
There are four constructors that you can use so as to create a
The simplest constructor which creates a
java.text.SimpleDateFormatwith a default pattern of date and a default locale.
The constructor which creates a
java.text.SimpleDateFormatwith a given pattern and a default locale.
SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
java.text.SimpleDateFormatwith the given pattern and specific date format symbols.
DateFormatSymbolsis a class for encapsulating localizable date-time formatting data, such as the names of the months, the names of the days of the week, and the time zone data.
SimpleDateFormat(String pattern, Locale locale)
java.text.SimpleDateFormatwith the given pattern and a specific locale.
2. Pattern Syntax
|Letter||Date or Time Component||Presentation||Examples|
|Month in year (context sensitive)||Month|
|Month in year (standalone form)||Month|
|Week in year||Number|
|Week in month||Number|
|Day in year||Number|
|Day in month||Number|
|Day of week in month||Number|
|Day name in week||Text|
|Day number of week (1 = Monday, …, 7 = Sunday)||Number|
|Hour in day (0-23)||Number|
|Hour in day (1-24)||Number|
|Hour in am/pm (0-11)||Number|
|Hour in am/pm (1-12)||Number|
|Minute in hour||Number|
|Second in minute||Number|
|Time zone||General time zone|
|Time zone||RFC 822 time zone|
|Time zone||ISO 8601 time zone|
3. Pattern Examples
|Date and Time Pattern||Result|
4. Example of SimpleDateFormat
Create a java class named
SimpleDateFormatExample.java with the following code:
Let’s explain the different formats of
SimpleDateFormat class in the above code. Firstly, we create a
Date object which is initialized with the current date and time. Then, we create different date formatters with different patterns, such as:
- The default pattern, which shows the date in the form of month/day/year and the time using the 12-hour clock.
yyyy/MM/dd, which shows the date in the form of year/month/day. As we can observe, the pattern for the year has 4 letters, which means that the full form of the year will be used (e.g. 2014). Otherwise a short or abbreviated form is used if available.
dd-M-yyyy hh:mm:ss, which shows the date in the form of date-month-year (the month will be shown in the abbreviated form, as it has only one letter and not two as in the previous case) and futhermore, it shows the time (hour, minutes and seconds) while the hour is in am/pm format.
dd MMMM yyyy zzzz, which shows the date and the timezone in full format. We can observe that we also defined the locale of the date/time:
E, dd MMM yyyy HH:mm:ss z, which shows the date, the day name of the week and the time (we can see that the hour is in capital, which means that the hour’s values here are between 0 – 23, as we use the 24-hour clock).
EEEEE dd MMMMM yyyy HH:mm:ss.SSSZ, which shows the day name, date, name of the month, year, 24H clock with seconds and 3 digits of milliseconds and timezone.
You may notice that there is a slight but basic difference to the followings:
mm:representes the minutes.
MM:represents the Month.
dd:represents the day.
DD:represents the day in year (e.g. 189 out of 365).
hh:represents the hour’s value using the 12-hour clock.
HH:represents the hour’s value using the 24-hour clock.
Using all those formatters, we format dates as strings.
Finally, we show a reverse example, where we parse a string into date, using the
If we run the above code, we will have the following results:Output
5. Download the Source CodeThis was an example of how to use the Java SimpleDateFormat class –
You can download the full source code of this example here: Java SimpleDateFormat Example
Last updated on Jan. 22, 2019