Java DateFormat format() Example

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


Syntax is : public final String format(Date date)
This method formats a Date into a date/time string.

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

class FormatDateFormat {

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

        // Create new DateFormat object that will returns default date/time.
        DateFormat dateFormat = DateFormat.getInstance();

        // format method call.
        String stringDate = dateFormat.format(date);
        System.out.println("format method call for specified date format.");
        System.out.println("Date by DateFormat class : " + stringDate);
    }
}

Output of the program.
Date  by  Date  class    : Thu Jun 12 17:58:21 GMT+05:30 2008
format method call for specified date format.
Date by DateFormat class : 6/12/08 5:58 PM

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

HOME | COPYRIGHT | CONTACT US