SimpleDateFormat toPattern() Method in Java with Examples

The toPattern() Method of SimpleDateFormat class is used to return the pattern of the date format.


public String toPattern()

Parameters: The method does not take any parameters.

Return Value: The method returns the pattern string describing this Date format.

Below programs illustrate the working of toPattern() Method of SimpleDateFormat:

Example 1:





// Java code to illustarte toPattern() 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 Calendar object
        Calendar cal = Calendar.getInstance();
        // Getting the Current Date
        String Todaysdate
            = SDformat.format(cal.getTime());
        // Displaying the date
        System.out.println("Current Date: "
                           + Todaysdate);
        // Using toPattern() method
        // to Print the Date Pattern
        System.out.println("The Date Pattern- "
                           + SDformat.toPattern());



Current Date: 1/29/19 6:05 AM
The Date Pattern- M/d/yy h:mm a

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.