java.text.SimpleDateFormat toPattern()

Description :

This java example shows how to use the toPattern()  method of SimpleDateFormat class of java.text package. This method returns a pattern string describing this date format.

Method Syntax :

public String toPattern()

Parameter Input :

DataType Parameter Description
N/A N/A N/A

Method Returns :

This method returns a pattern string describing this date format.

Compatibility Version :

Requires Java 1.2 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of toPattern() method of SimpledDateFormat class. The code just prints the date current pattern being used by the Formatter.

package com.javatutorialhq.java.examples;

import java.text.SimpleDateFormat;
import java.util.Calendar;


/*
 * This example source code demonstrates the use of  
 * toPattern() method 
 * of SimpleDateFormat class
 */

public class SimpleDateFormatToPatternExample {

	public static void main(String[] args) throws InterruptedException {
		
		// Initialize the formatter
		SimpleDateFormat sdf = new SimpleDateFormat();
		
		// initialize calendar object
		Calendar cal = Calendar.getInstance();
		
		// get the current date and time
		String dateToday = sdf.format(cal.getTime());
		
		// get the patter used by the formatter
		String pattern = sdf.toPattern();
		
		// printing date today
		System.out.println("Date today:"+dateToday);
		
		// printing the patter used
		System.out.println("Pattern:"+pattern);
	}

}

Sample Output :

Running the toPattern() method example source code of Calendar class will give you the following output:

SimpleDateFormat toPattern() method example

SimpleDateFormat toPattern() method example

Exception Scenario :

There are no notable exception that can be thrown by this method

Similar Method :

  • N/A

Suggested Reading List :

References :