Java SimpleDateFormat
and DateFormat
classes are used for date formatting. It is mostly used where we need to display or utilize the date and time functionality of Java. Both of these classes are present in com.text
package.
DateFormat
DateFormat
is used for formatting a date into String
based on specific locale that is provided as input.31st Dec 2017
will be written in India as 31-12-2017
but the same thing will be written in the United States of America as 12-31-2017
.DateFormat
classes are not synchronized, it’s recommended to create separate instance for each thread.A DateFormat
object can be created using the getDateInstance()
and getTimeInstance()
method of the DateFormat
class.
Locale loc = new Locale("en", "US");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, loc);
Note: The constructor Locale(String, String) is deprecated since version 19
Locale loc = new Locale.Builder().setLanguage("en").setRegion("US").build();
The getDateInstance
method of DateFormat
needs two input parameters. The first parameter specifies the DateFormat
to use and the second parameter is the locale.
format
DateFormat
class has a format
method which is responsible for formatting.
Locale locale = new Locale("fr", "FR");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);
This code will format the date in the French (fr
) language and the France (FR
) region:
Output3 janv. 2018
getTimeInstance()
For creating a DateFormat
instance we are using getDateInstance()
method.
For performing a time format, we need an instance of time. We will be using getTimeInstance()
method for getting an instance of time.
Locale locale = new Locale("fr", "FR");
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);
This code will format the time in the French (fr
) language and the France (FR
) region:
Output11:03:01
SimpleDateFormat
SimpleDateFormat
is similar to DateFormat
. The only major difference between them is that SimpleDateFormat
can be used for formatting (Date
to String
conversion) and for parsing (String
to Date
conversion) with locale support, whereas DateFormat
don’t have locale support. DateFormat
is an abstract class that provides base support for date formatting and parsing. SimpleDateFormat
is the concrete class that extends DateFormat
class.
SimpleDateFormat
can be created using the SimpleDateFormat
constructor. The constructor is a parametrised constructor and needs a String pattern as the parameter.
String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
The String pattern
is the pattern which will be used to format a date and the output will be generated in that pattern as “MM-dd-yyyy
”.
Let us have a look at the pattern syntax that should be used for the formatting pattern.
Letter for Pattern | Date or Time component | Examples |
---|---|---|
G | Era designator | AD |
y | Year | 2018 (yyyy), 18 (yy) |
M | Month in year | July (MMMM), Jul (MMM), 07 (MM) |
w | Results in week in year | 16 |
W | Results in week in month | 3 |
D | Gives the day count in the year | 266 |
d | Day of the month | 09 (dd), 9(d) |
F | Day of the week in month | 4 |
E | Day name in the week | Tuesday, Tue |
u | Day number of week where 1 represents Monday, 2 represents Tuesday and so on | 2 |
a | AM or PM marker | AM |
H | Hour in the day (0-23) | 12 |
k | Hour in the day (1-24) | 23 |
K | Hour in am/pm for 12 hour format (0-11) | 0 |
h | Hour in am/pm for 12 hour format (1-12) | 12 |
m | Minute in the hour | 59 |
s | Second in the minute | 35 |
S | Millisecond in the minute | 978 |
z | Timezone | Pacific Standard Time; PST; GMT-08:00 |
Z | Timezone offset in hours (RFC pattern) | -0800 |
X | Timezone offset in ISO format | -08; -0800; -08:00 |
Some letters should be used in different amount for different results like for month:
Type | Pattern | Example Output |
---|---|---|
Full Month | MMMM |
July |
Abbreviated Month | MMM |
Jul |
Numeric Month | MM |
07 |
Let us now look at some examples for different formats of date and time.
Pattern | Result |
---|---|
MM/dd/yyyy |
01/02/2018 |
dd-M-yyyy hh:mm:ss |
02-1-2018 06:07:59 |
dd MMMM yyyy |
02 January 2018 |
dd MMMM yyyy zzzz |
02 January 2018 India Standard Time |
E, dd MMM yyyy HH:mm:ss z |
Tue, 02 Jan 2018 18:07:59 IST |
SimpleDateFormat()
In order to parse a date we need to create an instance of SimpleDateFormat
using the constructor and then use the format()
method:
String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
This code will output “MM-dd-yyyy
”:
Output01-02-2018
This pattern produces “month in year”, “day in month”, and “year”. The type of character (and capitalization) and the amount of characters contribute to the string. The month would be represented as two digits, the day as two digits, and the year as four digits.
This code displayed the date of “January 2, 2018” as “01-02-2018
”.
For parsing time, we have to change the pattern while creating SimpleDateFormat
instance.
String pattern = "HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
This code will output “HH:mm:ss.SSSZ
”:
Output13:03:15.454+0530
This pattern produces “hours”, “minutes”, “seconds”, “milliseconds”, and “timezone offset in hours (RFC pattern)”.
This code displayed the time as “13:03:15.454+0530
”.
parse()
Parsing is the conversion of String into a java.util.Date
instance. We can parse a string to a date instance using parse()
method of the SimpleDateFormat class. For parsing a String to Date we need an instance of the SimpleDateFormat class and a string pattern as input for the constructor of the class.
String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("12-01-2018");
System.out.println(date);
This will output a date (12-01-2018
) without a specified time:
OutputSat Dec 01 00:00:00 IST 2018
Now let’s look at SimpleDateFormat
example to parse time.
String pattern = "HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("22:00:03");
System.out.println(date);
This will output a time (22:00:03
) without a specified date:
OutputThu Jan 01 22:00:03 IST 1970
Because we have not specified any date the program considered epoch as the date (i.e., 01-Jan-1970
).
Locale
We have worked with Locale
as part of the DateFormat
and we have seen that locales are used based on regions.
String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, new Locale("fr", "FR"));
Date date = simpleDateFormat.format(new Date());
System.out.println(date);
This code will format the current time in the French (fr
) language and the France (FR
) region:
Outputmardi janvier 2018 14:51:02.354+0530
The day and month are named in French based on the Locale
provided as input - mardi
is “Tuesday” and janvier
is “January”.
In this article, you learned about Java’s SimpleDateFormat
and DateFormat
.
Reference:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Wrong, “whereas DateFormat allows only formatting of Date.” it can be for parse string to Date
- sda
This is the best tutorial on dates I have ever seen
- Joe E
You Should Append About Calendar Class because Date is deprecated
- John Snow
How can i set date format 20th may, 2010
- sridhar
Thank you for the article, it’s been very helpful. I believe the day of the week should be amended in this manner below: EEEE, E Day name in the week Tuesday(EEEE), Tue(E or EEE)
- Shane McCurdy
You Should have addressed the important topic like Date conversion to different TimeZones. Anyway great tutorial.
- ManishS