Java SimpleDateFormat toPattern() Example
SimpleDateFormat class toPattern() method example. This example shows you how to use toPattern() method.
Syntax is : public String toPattern()
This method returns a pattern string describing this date format.
Here is the code.
/**
* @(#) ToPatternSimpleDateFormat.java
* A class representing use of method toPattern() of SimpleDateFormat
class in java.text Package.
* @Version 14-May-2008
* @author Rose India Team
*/
import java.util.*;
import java.text.*;
class ToPatternSimpleDateFormat {
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 by SimpleDateFormat class : " + dateFormat);
// toPattern method call for SimpleDateFormat object.
String pattern = sdf.toPattern();
System.out.println("Pattern string for this date format : " + pattern);
}
} |
Output of the program.
Date by Date class : Sat Jun 14 13:20:58 GMT+05:30 2008
Date by SimpleDateFormat class : 6/14/08 1:20 PM
Pattern string for this date format : M/d/yy h:mm a |
|