java.text.SimpleDateFormat applyPattern(String pattern)

Description :

This java example shows how to use the applyPattern(String pattern) method of SimpleDateFormat class of java.text package. This method applies the given pattern string to this date format.

Method Syntax :

public void applyPattern(String pattern)

Parameter Input :

DataType Parameter Description
String pattern the new date and time pattern for this date format

Method Returns :

This method returns void

Compatibility Version :

Requires Java 1.2 and up

Exception :

1. Below exception will be thrown if the given pattern is null.

NullPointerException

2. The following exception will be thrown if the given pattern is invalid

IllegalArgumentException

Java Code Example :

This java example source code demonstrates the use of applyPattern(String pattern) method of SimpledDateFormat class. The code just prints the date current localized pattern being used by the Formatter. This is preferred method over the toPattern() if you are dealing with multiple locale.

package com.javatutorialhq.java.examples;

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


/*
 * This example source code demonstrates the use of  
 * applyPattern(String pattern) method 
 * of SimpleDateFormat class
 */

public class ApplyPatternExample {

	public static void main(String[] args){
		
		// Initialize the formatter
		SimpleDateFormat sdf = new SimpleDateFormat();
		
		// initialize calendar object
		Calendar cal = Calendar.getInstance();
	
		// declare the patter and use it to the formatter
		String pattern = "yyyy/MM/dd HH:mm Z";
		sdf.applyPattern(pattern);
		
		// get the current date and time
		String dateToday = sdf.format(cal.getTime());
		
		// print the formatted date
		System.out.println("Date today:"+dateToday);
		
		// Print the pattern being used
		System.out.println("Pattern:"+sdf.toPattern());
	}

}

Sample Output :

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

SimpleDateFormat applyPattern(String pattern) method example

SimpleDateFormat applyPattern(String pattern) method example

Exception Scenario :

If we try to pass a null pattern as below code snippet:

String pattern = null;
sdf.applyPattern(pattern);

the following exception will be thrown

Exception in thread "main" java.lang.NullPointerException
	at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:726)
	at java.text.SimpleDateFormat.applyPatternImpl(SimpleDateFormat.java:2272)
	at java.text.SimpleDateFormat.applyPattern(SimpleDateFormat.java:2268)
	at com.javatutorialhq.java.examples.ApplyPatternExample.main(ApplyPatternExample.java:25)

 

Interestingly if we change the pattern into an invalid format like below

String pattern = ",lsdkw'/.xcas";
sdf.applyPattern(pattern);

running it, will give the following exception

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'l'
	at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:826)
	at java.text.SimpleDateFormat.applyPatternImpl(SimpleDateFormat.java:2272)
	at java.text.SimpleDateFormat.applyPattern(SimpleDateFormat.java:2268)
	at com.javatutorialhq.java.examples.ApplyPatternExample.main(ApplyPatternExample.java:25)

 

Similar Method :

  • N/A

Suggested Reading List :

References :