Monday, March 5, 2012

How do I get pattern string of a SimpleDateFormat in android?


To format a java.util.Date object we use the SimpleDateFormat class. To get back the string pattern that were used to format the date we can use the toPattern() method of this class.

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatToPattern {
    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("EEEE, dd/MM/yyyy");

        
        // Gets a pattern string describing this date format used by the
        // SimpleDateFormat object.
        
        String pattern = format.toPattern();

        System.out.println("Pattern = " + pattern);
        System.out.println("Date    = " + format.format(new Date()));
    }
}

Below is the output..

Pattern = EEEE, dd/MM/yyyy
Date    = Thursday, 16/06/2011

No comments:

Post a Comment