SimpleDateFormat applyLocalizedPattern() Method in Java with Examples

The applyLocalizedPattern() Method of SimpleDateFormat class is used to apply a localized string pattern to this date format. This pattern can be set by the user.
Syntax:Â
public void applyLocalizedPattern(String pattern)
Parameters: The method takes one parameter pattern of String type and refers to the new date and time that is to be applied.
Return Value: The method returns void type.
Below programs illustrate the working of applyLocalizedPattern() Method of SimpleDateFormat:
Example 1:Â
Java
// Java code to illustrate// applyLocalizedPattern() 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();Â
        // Using the following pattern        String new_pat = "MM / dd / yy HH:mm Z";Â
        // Use of applyLocalizedPattern()        SDFormat.applyLocalizedPattern(new_pat);Â
        System.out.println("Applying the format: "                           + SDFormat                                 .toLocalizedPattern());    }} |
Output:Â
Applying the format: MM / dd / yy HH:mm Z
Â
Example 2:
Java
// Java code to illustrate// applyLocalizedPattern() methodÂ
import java.text.*;import java.util.*;Â
public class SimpleDateFormat_Demo {    public static void main(String args[])        throws Exception    {Â
        // Initializing SDF        SimpleDateFormat SDFormat            = new SimpleDateFormat();Â
        // Applying LocalizedPattern        SDFormat.applyLocalizedPattern("dd");        String str = SDFormat.format(new Date());Â
        // Printing todays date        System.out.println("Today is: " + str);Â
        // Applying LocalizedPattern        SDFormat.applyLocalizedPattern("MMM");        String str1 = SDFormat.format(new Date());Â
        // Printing the month        System.out.println("Month is: " + str1);    }} |
Output:Â
Today is: 30 Month is: Jan
Â



