Java DateFormat getDateInstance() Example

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


Syntax is : public static final DateFormat getDateInstance(int style, Locale aLocale)
Method returns the date formatter with the given formatting style for the given locale.

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

class GetDateInstance2DateFormat {

    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();
        Locale locale = new Locale("ENGLISH");

        // getDateInstance method call.
        String dt = dateFormat.getDateInstance(DateFormat.SHORT, locale).
                format(date);
        System.out.println("SHORT style Date for specified locale : " + dt);
        dt = dateFormat.getDateInstance(DateFormat.LONG, locale).format(date);
        System.out.println("Date in LONG style for specified locale : " + dt);
        dt = dateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(date);
        System.out.println("Date in MEDIUM style for specified locale : " + dt);
        dt = dateFormat.getDateInstance(DateFormat.FULL, locale).format(date);
        System.out.println("Date in FULL style for specified locale : " + dt);
    }
}

Output of the program.
 Date  by  Date  class : Thu Jun 12 18:09:20 GMT+05:30 2008
SHORT style Date for specified locale : 6/12/08
Date in LONG style for specified locale : June 12, 2008
Date in MEDIUM style for specified locale : Jun 12, 2008
Date in FULL style for specified locale : Thursday, June 12, 2008

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

HOME | COPYRIGHT | CONTACT US