How do I get pattern string of a SimpleDateFormat?

To format a java.util.Date object we use the SimpleDateFormat class. To get back the string pattern that were used to format the date we can use the toPattern() method of this class.

package org.kodejava.text;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatToPattern {
    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("EEEE, dd/MM/yyyy");

        // Gets a pattern string describing this date format used by the
        // SimpleDateFormat object.
        String pattern = format.toPattern();

        System.out.println("Pattern = " + pattern);
        System.out.println("Date    = " + format.format(new Date()));
    }
}

The result of the program will be as follow:

Pattern = EEEE, dd/MM/yyyy
Date    = Tuesday, 26/10/2021

How do I change the date format symbols for a specified locale?

package org.kodejava.text;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ChangeDateFormatSymbols {
    public static void main(String[] args) {
        Locale id = new Locale("in", "ID");
        String pattern = "EEEE, dd MMM yyyy";
        Date today = new Date();

        // Gets formatted date specify by the given pattern for
        // Indonesian Locale no changes for default date format
        // is applied here.
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, id);
        String before = sdf.format(today);
        System.out.println("Before format change: " + before);

        // Create a DateFormatSymbols object for Indonesian locale.
        DateFormatSymbols dfs = new DateFormatSymbols(id);

        // Gets String array of default format of weekdays.
        String[] days = dfs.getWeekdays();
        String[] newDays = new String[days.length];
        for (int i = 0; i < days.length; i++) {
            // For each day, apply toUpperCase() method to
            // capitalized it.
            newDays[i] = days[i].toUpperCase();
        }

        // Set String array of weekdays.
        dfs.setWeekdays(newDays);

        // Gets String array of default format of short months.
        String[] shortMonths = dfs.getShortMonths();
        String[] months = new String[shortMonths.length];
        for (int j = 0; j < shortMonths.length; j++) {
            // For each short month, apply toUpperCase() method
            // to capitalized it.
            months[j] = shortMonths[j].toUpperCase();
        }

        // Set String array of short months.
        dfs.setShortMonths(months);

        // Create a SimpleDateFormat object by given pattern and 
        // symbol and then format the date object as String.
        sdf = new SimpleDateFormat(pattern, dfs);
        String after = sdf.format(today);
        System.out.println("After change format : " + after);
    }
}

Here are the output of our program:

Before format change: Selasa, 19 Okt 2021
After change format : SELASA, 19 OKT 2021

How do I get a formatted date for a specific pattern and locale?

If you want to change formatting styles provided by DateFormat, you can use SimpleDateFormat class. The SimpleDateFormat class is locale-sensitive.

If you instantiate SimpleDateFormat without a Locale parameter, it will format the date and time according to the default Locale. Both the pattern and the Locale determine the format. For the same pattern, SimpleDateFormat may format a date and time differently if the Locale varies.

package org.kodejava.text;

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

public class SimpleDateFormatChangeLocalePattern {
    public static void main(String[] args) {
        String pattern = "dd-MMM-yyyy";
        Date today = new Date();

        // Gets a formatted date according to the given pattern.
        // Here only the pattern is passed as argument of the
        // SimpleDateFormat constructor, so it will format the
        // date according to the default Locale.
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String local = sdf.format(today);
        System.out.println("Date in default locale: " + local);

        Locale[] locales = {
                Locale.CANADA,
                Locale.FRANCE,
                Locale.GERMANY,
                Locale.US,
                Locale.JAPAN
        };

        for (Locale locale : locales) {
            // Format a date according to the given pattern for each locale.
            sdf = new SimpleDateFormat(pattern, locale);
            String after = sdf.format(today);
            System.out.println(locale.getDisplayCountry() + " | format: " + after);
        }
    }
}

Here are the variety of output produces when formatting a date in the same date pattern but varies in Locale

Date in default locale: 19-Oct-2021
Canada | format: 19-Oct.-2021
France | format: 19-oct.-2021
Germany | format: 19-Okt.-2021
United States | format: 19-Oct-2021
Japan | format: 19-10月-2021

How do I format date using a locale based format?

The code below demonstrate how to format date information for a specific locale. In the example utilize the java.text.SimpleDateFormat class.

package org.kodejava.text;

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

public class FormatDateLocale {
    public static void main(String[] args) {
        // Defines an array of Locale we are going to use for
        // formatting date information.
        Locale[] locales = new Locale[] {
            Locale.JAPAN,
            Locale.CHINA,
            Locale.KOREA,
            Locale.TAIWAN,
            Locale.ITALY,
            Locale.FRANCE,
            Locale.GERMAN
        };

        // Get an instance of current date time
        Date today = new Date();

        // Iterates the entire Locale defined above and create a long
        // formatted date using the SimpleDateFormat.getDateInstance()
        // with the format, the Locale and the date information.
        for (Locale locale : locales) {
            System.out.printf("Date format in %s = %s%n",
                locale.getDisplayName(), SimpleDateFormat.getDateInstance(
                    SimpleDateFormat.LONG, locale).format(today));
        }
    }
}

The result of our code are:

Date format in Japanese (Japan) = 2021年10月6日
Date format in Chinese (China) = 2021年10月6日
Date format in Korean (South Korea) = 2021년 10월 6일
Date format in Chinese (Taiwan) = 2021年10月6日
Date format in Italian (Italy) = 6 ottobre 2021
Date format in French (France) = 6 octobre 2021
Date format in German = 6. Oktober 2021

How do I compare two dates?

In this example you’ll see the utilization of SimpleDateFormat class for comparing two dates for equality. We convert the date object into string using the DateFormat instance and then compare it using String.equals() method.

package org.kodejava.util;

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

public class ComparingDate {
    public static void main(String[] args) {
        // Create a SimpleDateFormat instance with dd/MM/yyyy format
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

        // Get the current date
        Date today = new Date();

        // Create an instance of calendar that represents a new year date
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.YEAR, 2021);

        String newYear = df.format(calendar.getTime());
        String now = df.format(today);

        // Using the string equals method we can compare the date.
        if (now.equals(newYear)) {
            System.out.println("Happy New Year!");
        } else {
            System.out.println("Have a nice day!");
        }
    }
}