SimpleDateFormat applyPattern() Method in Java with Examples
The applyPattern() Method of SimpleDateFormat class is used to set a given defined pattern to the Date Format. It simply converts a particular date and time to a specific format as defined by the user for eg., dd/ MM/ yyyy HH:mm Z or MM/ dd/ yyyy HH:mm Z.
Syntax:
public void applyPattern(String pattern)
Parameters: The method takes one parameter pattern of String type and refers to the new date and time pattern for this date format.
Return Value: The method returns void type.
Below programs illustrate the working of applyPattern() Method of SimpleDateFormat:
Example 1:
// Java code to illustrate // applyPattern() method import java.text.*; import java.util.Calendar; public class SimpleDateFormat_Demo { public static void main(String[] args) throws InterruptedException { SimpleDateFormat SDFormat = new SimpleDateFormat(); // Initializing the calender Object Calendar cal = Calendar.getInstance(); // Using the below pattern String new_pat = "dd/ MM/ yyyy HH:mm Z"; // Use of applyPattern() method SDFormat.applyPattern(new_pat); // Displaying Current date and time String curr_date = SDFormat.format(cal.getTime()); System.out.println("The Current Date: " + curr_date); // Displaying the pattern System.out.println("Applied Pattern: " + SDFormat.toPattern()); } } |
The Current Date: 29/ 01/ 2019 07:22 +0000 Applied Pattern: dd/ MM/ yyyy HH:mm Z
Example 2:
// Java code to illustrate // applyPattern() method import java.text.*; import java.util.Calendar; public class SimpleDateFormat_Demo { public static void main(String[] args) throws InterruptedException { SimpleDateFormat SDFormat = new SimpleDateFormat(); // Initializing the calender Object Calendar cal = Calendar.getInstance(); // Using the below pattern String new_pat = "MM/ dd/ yyyy HH:mm Z"; // Use of applyPattern() method SDFormat.applyPattern(new_pat); // Displaying Current date and time String curr_date = SDFormat.format(cal.getTime()); System.out.println("The Current Date: " + curr_date); // Displaying the pattern System.out.println("Applied Pattern: " + SDFormat.toPattern()); } } |
The Current Date: 01/ 29/ 2019 07:22 +0000 Applied Pattern: MM/ dd/ yyyy HH:mm Z
Recommended Posts:
- ChoiceFormat applyPattern() method in Java with Examples
- SimpleDateFormat get2DigitYearStart() Method in Java with Examples
- SimpleDateFormat format() Method in Java with Examples
- SimpleDateFormat applyLocalizedPattern() Method in Java with Examples
- SimpleDateFormat clone() Method in Java with Examples
- SimpleDateFormat setDateFormatSymbols() Method in Java with Examples
- SimpleDateFormat hashCode() Method in Java with Examples
- SimpleDateFormat equals() Method in Java with Examples
- SimpleDateFormat getDateFormatSymbols() Method in Java with Examples
- SimpleDateFormat toPattern() Method in Java with Examples
- SimpleDateFormat set2DigitYearStart() Method in Java with Examples
- SimpleDateFormat parse() Method in Java with Examples
- SimpleDateFormat toLocalizedPattern() Method in Java with Examples
- DecimalFormat applyPattern() method in Java
- Java SimpleDateFormat | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

