January 1, 2023

Java DateFormat Class

java.text.DateFormat is an abstract class which acts as a super class for date/time formatting subclasses which do the task of-

  • Formatting dates or time- Usually Date object is passed and a formatted String is returned.
  • Parsing dates or time- String is passed and a parsed Date object is returned.

Getting DateFormat instance

In DateFomat class there are static factory methods to get the date formatter, time formatter or a date time formatter.

For getting the date formatter-

  • getDateInstance()- Gets the date formatter with the default formatting style for the default FORMAT locale.
  • getDateInstance(int style)- Gets the date formatter with the given formatting style for the default FORMAT locale.
  • getDateInstance(int style, Locale aLocale)- Gets the date formatter with the given formatting style for the given locale.

For getting the time formatter-

  • getTimeInstance()- Gets the time formatter with the default formatting style for the default FORMAT locale.
  • getTimeInstance(int style)- Gets the time formatter with the given formatting style for the default FORMAT locale.
  • getTimeInstance(int style, Locale aLocale)- Gets the time formatter with the given formatting style for the given locale.

For getting the date/time formatter-

  • getDateTimeInstance()- Gets the date/time formatter with the default formatting style for the default FORMAT locale.
  • getDateTimeInstance(int dateStyle, int timeStyle)- Gets the date/time formatter with the given date and time formatting styles for the default FORMAT locale.
  • getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)- Gets the date/time formatter with the given formatting styles for the given locale.

For formatting styles there are constant fields defined in DateFormat class-

DateFormat.FULL = 0
DateFormat.LONG	= 1
DateFormat.MEDIUM = 2
DateFormat.DEFAULT = 2 (Its value is MEDIUM.)
DateFormat.SHORT = 3

Formatting date examples

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

public class DFExample {

  public static void main(String[] args) {
    Date date = new Date();
    DateFormat df;
    
    // Default settings
    df = DateFormat.getDateInstance();
    System.out.println("Date with default settings- " + df.format(date));
    
    // Style FULL and Locale
    df = DateFormat.getDateInstance(DateFormat.FULL, Locale.FRANCE);
    System.out.println("Full Date (France)- " + df.format(date));
    
    // Style Long and Locale
    df = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
    System.out.println("Long Date (US)- " + df.format(date));
    
    // Style Medium, default Locale
    df = DateFormat.getDateInstance(DateFormat.MEDIUM);
    System.out.println("Medium Date- " + df.format(date));
  }
}
Output
Date with default settings- 06-Oct-2019
Full Date (France)- dimanche 6 octobre 2019
Long Date (US)- October 6, 2019
Medium Date- 06-Oct-2019

Formatting time examples

You can get only the time instance using the getTimeInstance() methods and format it.

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

public class DFExample {

  public static void main(String[] args) {
    Date date = new Date();
    DateFormat df;
    
    // Default settings
    df = DateFormat.getTimeInstance();
    System.out.println("Time with default settings- " + df.format(date));
    
    // Style FULL and Locale
    df = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRANCE);
    System.out.println("Full Time (France)- " + df.format(date));
    
    // Style Long and Locale
    df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.US);
    System.out.println("Long time (US)- " + df.format(date));
    
    // Style Medium, default Locale
    df = DateFormat.getTimeInstance(DateFormat.MEDIUM);
    System.out.println("Medium Time- " + df.format(date));
  }
}
Output
Time with default settings- 12:03:49 PM
Full Time (France)- 12:03:49 heure de l’Inde
Long time (US)- 12:03:49 PM IST
Medium Time- 12:03:49 PM

Formatting date and time examples

You can format both date and time by getting the datetime instance. In that case for formatting you need to pass both date and time style parameters.

public class DFExample {

  public static void main(String[] args) {
    Date date = new Date();
    DateFormat df;
    
    // Default settings
    df = DateFormat.getDateTimeInstance();
    System.out.println("Date time with default settings- " + df.format(date));
    
    // Style FULL and Locale
    df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.FRANCE);
    System.out.println("Full Date time (France)- " + df.format(date));
    
    // Style Long and Locale
    df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, Locale.US);
    System.out.println("Long Date time (US)- " + df.format(date));
    
    // Style Medium, default Locale
    df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    System.out.println("Medium Date time- " + df.format(date));
  }
}
Output
Date time with default settings- 06-Oct-2019, 12:08:09 PM
Full Date time (France)- dimanche 6 octobre 2019 à 12:08:09 heure de l’Inde
Long Date time (US)- October 6, 2019, 12:08:09 PM
Medium Date time- 06-Oct-2019, 12:08:09 PM

Setting time zone

You can also set time zone for the DateFormat object.

Date date = new Date();	
// Style Long and Locale
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, Locale.US);
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println("Long Date time (US)- " + df.format(date));

DateFormat Synchronization

Date formats are not synchronized. If multiple threads access DateFormat instance concurrently, it must be synchronized externally.

That's all for the topic Java DateFormat Class. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment