In Java, there can be scenarios where there can be a requirement to return the date or time in accordance with a particular pattern. For instance, returning the date and time in accordance with a specified region format. In such situations, the “SimpleDateFormat” class allows the developer to both format and parse the date accordingly.
This write-up will elaborate on the working of the “SimpleDateFormat” class in Java.
What is SimpleDateFormat in Java?
The “SimpleDateFormat” corresponds to a class that is utilized for “formatting” i.e., “converting Date to String” and for “parsing” i.e., “converting String to Date”. It is such that it is used to both parse and format the dates according to the specified formatting pattern. Also, the “SimpleDateFormat” class is identical to “DateFormat”. The only difference between them is that the former approach can be used with “locale” support, whereas it is not the case in the “DateFormat” class.
The “SimpleDateFormat” class comprises a parameterized constructor and needs a “String” pattern as its parameter. The most common pattern parameters and their outcomes are listed below:
Pattern | Outcome |
MM/dd/yyyy | 01/02/2018 |
dd-M-yyyy hh:mm:ss | 02-1-2018 06:07:59 |
dd MMMM yyyy zzzz | 02 January 2018 Time Zone |
Include the following packages before moving on to the examples to access all the classes within the “java.util” package and working with the “SimpleDateFormat” class, respectively:
import java.text.SimpleDateFormat;
Example 1: Applying the “SimpleDateFormat” Class to Return the Current Date
This example applies the “SimpleDateFormat” class to return the current date in accordance with the specified pattern:
public static void main(String args[]){
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
String date = dateFormat.format(new Date());
System.out.println("The current Date is -> " +date);
}}
In the above code snippet:
- Firstly, create a “SimpleDateFormat” object using the “new” keyword and the “SimpleDateFormat()” constructor.
- Also, specify the stated pattern as a constructor parameter in which the current date needs to be displayed.
- Lastly, associate the “format()” method to return the current date as a string.
Output
In this output, it can be analyzed that the current Date is returned appropriately.
To parse the specified Date i.e., “String to Date” instead, declare the “ParseException” and utilize the “parse()” method, thereby changing the return type to “Date”, as follows:
Example 2: Applying the “SimpleDateFormat” Class to Return the Date and Time For a Specific Locale
This particular example utilizes the “SimpleDateFormat” class to return the current date and time according to the specified “Locale”:
public static void main(String args[]){
String pat = "EEEEE dd MMMMM yyyy HH:mm:ss.SSSZ";
SimpleDateFormat dateFormat = new SimpleDateFormat(pat, new Locale("da", "DK"));
String date = dateFormat.format(new Date());
System.out.println("The Date and Time for the specified Locale are -> " +date);
}}
In the above code lines:
- Firstly, initialize the string pattern that needs to be followed for returning the date and time.
- After that, likewise create a “SimpleDateFormat” object having the defined pattern and the “Locale” as its arguments, respectively.
- Finally, apply the “format()” method to display the current date and time in the form of a string.
Output
In this outcome, it can be implied that “Friday” is specified as “fredag” and “May” as “maj” in “Danish” as per the specified “Locale”.
Conclusion
The “SimpleDateFormat” class is utilized for formatting i.e., “converting Date into String” and for parsing i.e., “converting String into Date. This methodology has an added advantage over the “DateFormat” class since it also works with the “Locale”. This blog demonstrated the working of the “SimpleDateFormat” class in Java.