Java SimpleDateFormat applyPattern() Example

SimpleDateFormat class applyPattern() method example. This example shows you how to use applyPattern() method.


Syntax is : public void applyPattern(String pattern)
This method applies the given pattern string to this date format.

Here is the code.
/**
 * @(#) ApplyPatternSimpleDateFormat.java
 * A class representing use of method applyPattern() of SimpleDateFormat
class in java.text Package.
 * @Version  13-May-2008
 @author   Rose India Team
 */
import java.util.*;
import java.text.*;

class ApplyPatternSimpleDateFormat {

    public static void main(String[] args) {

        // Create new Date object that will be initialized to the current date.
        Date date = new Date();
        System.out.println("Date  by  Date  class         : "+ date.toString());
        SimpleDateFormat sdf;

        // Create new SimpleDateFormat object that returns default pattern.
        sdf = new SimpleDateFormat();
        String dateFormat = sdf.format(date);
        System.out.println("Date in dafault pattern       : " + dateFormat);

        // applyPattern method call.
        sdf.applyPattern("hh:mm:ss");
        dateFormat = sdf.format(date);
        System.out.println("Date in hh:mm:ss pattern      : " + dateFormat);

        sdf.applyPattern("E MMM dd yyyy");
        dateFormat = sdf.format(date);
        System.out.println("Date in E MMM dd yyyy pattern : " + dateFormat);
    }
}

Output of the program.
Date  by  Date  class         : Fri Jun 13 18:54:21 GMT+05:30 2008
Date in dafault pattern       : 6/13/08 6:54 PM
Date in hh:mm:ss pattern      : 06:54:21
Date in E MMM dd yyyy pattern : Fri Jun 13 2008

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US