DateFormat Class

DateFormat Class

In this tutorial, we are going to discuss about DateFormat Class in java. DateFormat class can be used for formatting the dates according to a particular locale. DateFormat class is available in java.text package.

DateFormat Class

DateFormat class is an abstract class we can’t create an object by using the constructor.

DateFormat df = new DateFormat(); // C.E
Creation of DateFormat Object for a default locale

DateFormat class contains the following methods to get DateFormat Objects.

public static DateFormat getInstance();
public static DateFormat getDateInstance();
public static DateFormat getDateInstance(int style)

Where style is DateFormats

FULL – 0
LONG-1
MEDIUM-2
SHORT-3
Creation of DateFormat Object for a specific Locale
public static DateFormat getDateInstance(int style, Locale l)

DateFormat class contain the following method for converting java Date form to Locale specific form.

String format(Date d)

DateFormat class contain the following method for converting Locale Specific form to java Date form.

Date parse(String s)throws parseException
package com.ashok.internationalization;

import java.text.DateFormat;
import java.util.Date;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyDateFormat {
   public static void main(String[] args) {
      System.out.println("Full form is -->" + DateFormat.getDateInstance(0).format(new Date()));
      System.out.println("Long form is -->" + DateFormat.getDateInstance(1).format(new Date()));
      System.out.println("Medium form is -->" + DateFormat.getDateInstance(2).format(new Date()));
      System.out.println("Short form is -->" + DateFormat.getDateInstance(3).format(new Date()));
   }
}

Output

Full form is -->Thursday, September 1, 2016
Long form is -->September 1, 2016
Medium form is -->Sep 1, 2016
Short form is -->9/1/16

Program for displaying the current date according to UK,US and ITALY specific ways.

package com.ashok.internationalization;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * @author ashok.mariyala
 *
 */
class MyDateFormat {
   public static void main(String[] args) {
      DateFormat UK = DateFormat.getDateInstance(0, Locale.UK);
      DateFormat US = DateFormat.getDateInstance(0, Locale.US);
      DateFormat ITALY = DateFormat.getDateInstance(0, Locale.ITALY);
      System.out.println("UK Style is --> " + UK.format(new Date()));
      System.out.println("US Style is --> " + US.format(new Date()));
      System.out.println("ITALY Style is --> " + ITALY.format(new Date()));
   }
}

Output

UK Style is --> Thursday, 1 September 2016
US Style is --> Thursday, September 1, 2016
ITALY Style is --> giovedì 1 settembre 2016
Creation of DateFormat Object for representing both Date and Time

The following are the constructors for representing both Data and Time.

public static DateFormat getDateTimeInstance();
public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle);
public static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale l)
package com.ashok.internationalization;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * @author ashok.mariyala
 *
 */
class MyDateFormat {
   public static void main(String[] args) {
      DateFormat ITALY = DateFormat.getDateTimeInstance(0,0,Locale.ITALY);
      System.out.println("ITALY Style is --> " + ITALY.format(new Date()));
   }
}

Output

ITALY Style is --> giovedì 1 settembre 2016 16.47.06 IST

That’s all about the DateFormat class in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

DateFormat Class
Scroll to top