SimpleDateFormat toPattern() Method in Java with Examples

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

Syntax:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

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


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 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.