How do I format a message that contains date information?

This example demonstrate how you can use the java.text.MessageFormat class to format a message with a date information in it.

package org.kodejava.example.text;

import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import java.text.MessageFormat;

public class MessageFormatDate {
    public static void main(String[] args) {
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 7);

        Date nextWeek = calendar.getTime();

        // We want the message to be is Locale.US
        Locale.setDefault(Locale.US);

        // Format a date, the time value is included
        String message = MessageFormat.format("Today is {0} and next " +
            "week is {1}", today, nextWeek);
        System.out.println(message);

        // Format a date and display only the date portion
        message = MessageFormat.format("Today is {0,date} and next " +
            "week is {1,date}", today, nextWeek);
        System.out.println(message);

        // Format a date using a short format (eg. dd/MM/yyyy)
        message = MessageFormat.format("Today is {0,date,short} and " +
            "next week is {1,date,short}", today, nextWeek);
        System.out.println(message);

        // Format a date using a medium format, it display the month long name,
        // but using a two digit date and year.
        message = MessageFormat.format("Today is {0,date,medium} and " +
            "next week is {1,date,medium}", today, nextWeek);
        System.out.println(message);

        // Format a date using a long format, two digit for date, a long month
        // name and a four digit year.
        message = MessageFormat.format("Today is {0,date,long} and " +
            "next week is {1,date,long}", today, nextWeek);
        System.out.println(message);

        // Format a date using a full format, the same as above plus a full day
        // name.
        message = MessageFormat.format("Today is {0,date,full} and " +
            "next week is {1,date,full}", today, nextWeek);
        System.out.println(message);

        // Format a date using a custom pattern.
        message = MessageFormat.format("Today is {0,date,dd-MM-yyyy} and " +
            "next week is {1,date,dd-MM-yyyy}", today, nextWeek);
        System.out.println(message);
    }
}

The above program produces:

Today is 2/15/18, 8:57 PM and next week is 2/22/18, 8:57 PM
Today is Feb 15, 2018 and next week is Feb 22, 2018
Today is 2/15/18 and next week is 2/22/18
Today is Feb 15, 2018 and next week is Feb 22, 2018
Today is February 15, 2018 and next week is February 22, 2018
Today is Thursday, February 15, 2018 and next week is Thursday, February 22, 2018
Today is 15-02-2018 and next week is 22-02-2018

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA.

Leave a Reply