java.text.SimpleDateFormat
is a concrete implementation of DateFormat class
for formatting and parsing dates as per the passed formatting pattern. For example
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
shows the formatted date in the form- Wednesday, July 15, 2019
Java SimpleDateFormat constructors
There are four constructors in SimpleDateFormat class.
- SimpleDateFormat()- Constructs a SimpleDateFormat using the default pattern and date format symbols for the default FORMAT locale.
- SimpleDateFormat(String pattern)- Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default FORMAT locale.
- SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)- Constructs a SimpleDateFormat using the given pattern and date format symbols.
- SimpleDateFormat(String pattern, Locale locale)- Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the given locale.
Java SimpleDateFormat Date and Time patterns
Date and time formats are specified by date and time pattern strings. Following list shows the pattern letters that are used with SimpleDateFormat.
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G | Era designator | Text | AD |
y | Year | Year | 1996; 96 |
Y | Week year | Year | 2009; 09 |
M | Month in year (context sensitive) | Month | July; Jul; 07 |
L | Month in year (standalone form) | Month | July; Jul; 07 |
w | Week in year | Number | 27 |
W | Week in month | Number | 2 |
D | Day in year | Number | 189 |
d | Day in month | Number | 10 |
F | Day of week in month | Number | 2 |
E | Day name in week | Text | Tuesday; Tue |
u | Day number of week (1 = Monday, ..., 7 = Sunday) | Number | 1 |
a | Am/pm marker | Text | PM |
H | Hour in day (0-23) | Number | 0 |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | 0 |
h | Hour in am/pm (1-12) | Number | 12 |
m | Minute in hour | Number | 30 |
s | Second in minute | Number | 55 |
S | Millisecond | Number | 978 |
z | Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |
X | Time zone | ISO 8601 time zone | -08; -0800; -08:00 |
Rules that are followed for the patterns are as follows-
1. For text if the number of pattern letters is 4 or more full form is used otherwise a short or abbreviated form is used if available. For example, a EEEE pattern can display full name of weekday like Wednesday and a EEE pattern can display Wed.
2. For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount.
For numbers, the number of times a pattern letter is repeated determines the minimum number of digits, for shorter numbers zero-padding is done if required. For example, hh:mm:ss shows 03:45:15, but h:m:s displays the same time as 3:45:15.
3. For month, If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
Formatting date using SimpleDateFormat examples
After creating an instance of SimpleDateFormat with the specified pattern, using the format()
method you can format
the date.
import java.text.SimpleDateFormat; import java.util.Date; public class FormatDate { public static void main(String[] args) { // For date in format 2019.07.04 AD at 11:08:54 IST formatDate("yyyy.MM.dd G 'at' HH:mm:ss z"); // For date in format Mon, Oct 7, '19 formatDate("EEE, MMM d, ''yy"); // For date in format Monday, October 07, 2019 formatDate("EEEE, MMMM dd, yyyy"); // For time in format 07 o'clock PM, India Standard Time formatDate("hh 'o''clock' a, zzzz"); // For time in 24 Hr format 19:41:59:635 PM formatDate("HH:mm:ss:SSS a"); // For date-time in format 2019-10-07T19:27:38.571+0530 formatDate("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); // For date in format 05/08/2016 formatDate("MM/dd/yyyy"); // For date in format 07/10/2019 19:29:40 PM formatDate("dd/MM/yyyy HH:mm:ss a"); // For date in format 07/10/2019 19:29:40 PM formatDate("dd/MMM/yyyy GGG HH:mm:ss:SSS a"); } private static void formatDate(String pattern){ Date dt = new Date(); // Create date format as per specified pattern SimpleDateFormat sdf = new SimpleDateFormat(pattern); String formattedDate = sdf.format(dt); System.out.println("Formatted Date- " + formattedDate + " for Pattern: " + pattern); } }Output
Formatted Date- 2019.10.07 AD at 19:41:59 IST for Pattern: yyyy.MM.dd G 'at' HH:mm:ss z Formatted Date- Mon, Oct 7, '19 for Pattern: EEE, MMM d, ''yy Formatted Date- Monday, October 07, 2019 for Pattern: EEEE, MMMM dd, yyyy Formatted Date- 07 o'clock PM, India Standard Time for Pattern: hh 'o''clock' a, zzzz Formatted Date- 19:41:59:635 PM for Pattern: HH:mm:ss:SSS a Formatted Date- 2019-10-07T19:41:59.638+0530 for Pattern: yyyy-MM-dd'T'HH:mm:ss.SSSZ Formatted Date- 10/07/2019 for Pattern: MM/dd/yyyy Formatted Date- 07/10/2019 19:41:59 PM for Pattern: dd/MM/yyyy HH:mm:ss a Formatted Date- 07/Oct/2019 AD 19:41:59:641 PM for Pattern: dd/MMM/yyyy GGG HH:mm:ss:SSS a
Parsing date using SimpleDateFormat
Using parse()
method of SimpleDateFormat you can convert String to a java.util.Date instance.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ParseDate { public static void main(String[] args) { try { parse("dd/MM/yyyy", "09/08/2019"); parse("MM-dd-yyyy", "09-08-2019"); // Date will default to epoch (January 1, 1970) parse("HH:mm:ss", "20:04:19"); parse("MM-dd-yyyy HH:mm:ss", "09-08-2019 20:04:19"); }catch (ParseException e) { System.out.println("Error while parsing- " + e.getMessage()); } } private static void parse(String pattern, String dateTime) throws ParseException{ // Create date format as per specified pattern SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date dt = sdf.parse(dateTime); System.out.println("After parsing- " + dt); } }Output
After parsing- Fri Aug 09 00:00:00 IST 2019 After parsing- Sun Sep 08 00:00:00 IST 2019 After parsing- Thu Jan 01 20:04:19 IST 1970 After parsing- Sun Sep 08 20:04:19 IST 2019
That's all for the topic Java SimpleDateFormat Class. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Convert Date to String in Java
- Convert String to Date in Java
- LocalDate in Java With Examples
- final in Java With Examples
- JDBC Connection Interface
- Private Methods in Java Interface
- Injecting List, Set or Map in Spring
- Spring + JPA (Hibernate) OneToMany Example
- React Form Creation and Validation Using React Hooks
No comments:
Post a Comment