Java SimpleDateFormat format() Example

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


Syntax is : public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos)
This method formats the given Date into a date/time string and appends the result to the given StringBuffer.

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

class FormatSimpleDateFormat {

    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);

        // format method call.
        StringBuffer strBuffer = new StringBuffer();
        FieldPosition field = new FieldPosition(DateFormat.DEFAULT);
        strBuffer = sdf.format(date, strBuffer, field);
        System.out.println("format method call for specified date format.");
        System.out.println("Date by SimpleDateFormat class : " + strBuffer);
    }
}

Output of the program.
Date  by  Date  class          : Fri Jun 13 18:58:37 GMT+05:30 2008
Date in dafault pattern        : 6/13/08 6:58 PM
format method call for specified date format.
Date by SimpleDateFormat class : 6/13/08 6:58 PM

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

HOME | COPYRIGHT | CONTACT US