Java - Format date to your locale settings

By xngo on June 28, 2019

Java provides four different default formats for formatting and parsing locale dates. There are:

  • SHORT is completely numeric, such as 12.13.52 or 3:30pm
  • MEDIUM is longer, such as Jan 12, 1952
  • LONG is longer, such as January 12, 1952 or 3:30:32pm
  • FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.

Here are the examples.

import java.util.Date;
import java.text.DateFormat;
 
public class LocaleDate {
 
    public static void main(String[] args) {
 
        // Get today's date.
        Date today = new Date();
 
        String d = DateFormat.getDateInstance(DateFormat.SHORT).format(today);
        System.out.println("SHORT  : " + d);
 
        d = DateFormat.getDateInstance(DateFormat.MEDIUM).format(today);
        System.out.println("MEDIUM : " + d);
 
        d = DateFormat.getDateInstance(DateFormat.LONG).format(today);
        System.out.println("LONG   : " + d);
 
        d = DateFormat.getDateInstance(DateFormat.FULL).format(today);
        System.out.println("FULL   : " + d);
 
        d = DateFormat.getDateInstance(DateFormat.DEFAULT).format(today);
        System.out.println("DEFAULT: " + d);
 
    }
}

Output

Note: Depending on your locale, your output may differ from mine.

SHORT  : 6/28/19
MEDIUM : Jun 28, 2019
LONG   : June 28, 2019
FULL   : Friday, June 28, 2019
DEFAULT: Jun 28, 2019

Parse locale date format

Below is an example showing how to parse FULL locale date format.

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
 
public class LocaleDateParse {
 
    public static void main(String[] args) {
 
        // Parse
        try {
            Date date = DateFormat.getDateInstance(DateFormat.FULL).parse("Friday, June 28, 2019");
            System.out.println("Parsed FULL locale date format : " + date);
 
        } catch (ParseException e) {
            System.out.println(e);
        }
    }
}

Output

Parsed FULL locale date format : Fri Jun 28 00:00:00 UTC 2019

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.