Java DateFormat format() Example
DateFormat class format() method example. This example shows you how to use format() method.
Syntax is : public abstract StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)
This method formats a Date into a date/time string.
Here is the code.
/**
* @(#) Format1DateFormat.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 Format1DateFormat {
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.
StringBuffer strBuffer = new StringBuffer();
FieldPosition field = new FieldPosition(DateFormat.DEFAULT);
strBuffer = dateFormat.format(date, strBuffer, field);
System.out.println("format method call for specified date format.");
System.out.println("Date by DateFormat class : " + strBuffer);
}
} |
Output of the program.
Date by Date class : Thu Jun 12 17:52:55 GMT+05:30 2008
format method call for specified date format.
Date by DateFormat class : 6/12/08 5:52 PM |
|