Formatting Dates

The previous sections have given you an appreciation for the way dates can be created and manipulated. This section will address the manner in which dates can be converted into Strings for display.

Java uses the concept of formatters for converting objects or primitives into Strings for display. These same formatters are also capable of acting in reverse: accepting a String and parsing it to produce an object or primitive of the appropriate type.

With dates, the base interface responsible for formatting is DateFormat, and its principle implementation is SimpleDateFormat.

In order to operate on Dates, the DateFormat needs to know the pattern the textual String should adhere to. The following are all valid representations of the same date (although to varying degrees of accuracy):

  • 26 January 2015 15:34:00

  • 2015-1-26 3:34pm

  • Jan 26, 2015

  • 2015

  • January 15

Java provides an expression language that can be used to define date formats.

The following is an example of a program that uses a Calendar to construct a Date, and then uses an instance of SimpleDateFormat to print the date in the formats listed:

package dates;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

 

public class Formatting {

    public static void main(String[] args) {

        Calendar c = new GregorianCalendar(2014, 0, 26, 15, 34, 0);

        Date date = c.getTime();

        DateFormat df1 = new SimpleDateFormat("dd MMMM yyyy HH:mm:ss");

        System.out.println(df1.format(date));

           

        df1 = new SimpleDateFormat("yyyy-M-dd h:mm aa");

        System.out.println(df1.format(date));

           

        df1 = new SimpleDateFormat("MMM dd, yyyy");

        System.out.println(df1.format(date));

           

        df1 = new SimpleDateFormat("yyyy");

        System.out.println(df1.format(date));

           

        df1 = new SimpleDateFormat("MMMM yy");

        System.out.println(df1.format(date));

    }

}

 

I will not provide a full description of the formatting rules, because these can easily be looked up when needed. There are, however, a few key rules to remember:

  • Each component of a date (e.g. hours, days, year) is represented by a letter, but these are case sensitive: “M” represents month, “m” represents minutes, “s” represents seconds. This is a common source of bugs, at least for myself.

  • The number of times the letter is repeated may impact the presentation. For instance MMMM produces January, MMM produces Jan, MM produces 01, M produces 1.

  •  It is possible to interweave date components with other formatting symbols such as “-“ and “:”.

Java also supports a set of “default” formats based on the user’s locale (the concept of locales will be discussed in future chapters). For instance, the following can be used to print the day, month and year in a format appropriate to the user running the program:

DateFormat df2 = DateFormat.getDateInstance(DateFormat.SHORT);

System.out.println(df2.format(date));

The constants MEDIUM, LONG and FULL can also be used, and each provides different output.

As mentioned, you can also use SimpleDateFormat to convert Strings into Dates. The following program demonstrates this:

public static void main(String[] args) throws ParseException {

    Calendar c = new GregorianCalendar(2015, 0, 26, 15, 34, 0);

    Date date = c.getTime();

    DateFormat df1 = new SimpleDateFormat("dd MMMM yyyy HH:mm:ss");

    System.out.println(df1.parse("26 January 2015 15:34:00"));

           

    df1 = new SimpleDateFormat("yyyy-M-dd h:mm aa");

    System.out.println(df1.parse("2015-1-26 3:34 PM"));

           

    df1 = new SimpleDateFormat("MMM dd, yyyy");

    System.out.println(df1.parse("Jan 26, 2015"));

           

    df1 = new SimpleDateFormat("yyyy");

    System.out.println(df1.parse("2015"));

           

    df1 = new SimpleDateFormat("MMMM yy");

    System.out.println(df1.parse("January 15"));

}

This is an interesting example, because in many of these cases the format does not provide a value for all elements of the date. For instance, in the second last case the only date element you are providing is the year. If a value is not provided for a date component, Java simply defaults in the first value; if no month is provided it is set to January. The example above prints out:

Sun Jan 26 15:34:00 NZDT 2015

Sun Jan 26 15:34:00 NZDT 2015

Sun Jan 26 00:00:00 NZDT 2015

Wed Jan 01 00:00:00 NZDT 2015

Wed Jan 01 00:00:00 NZDT 2015

 

 
A Software Engineer Learns Java and Object Orientated Programming
titlepage.xhtml
part0000_split_000.html
part0000_split_001.html
part0000_split_002.html
part0000_split_003.html
part0000_split_004.html
part0000_split_005.html
part0000_split_006.html
part0000_split_007.html
part0000_split_008.html
part0000_split_009.html
part0000_split_010.html
part0000_split_011.html
part0000_split_012.html
part0000_split_013.html
part0000_split_014.html
part0000_split_015.html
part0000_split_016.html
part0000_split_017.html
part0000_split_018.html
part0000_split_019.html
part0000_split_020.html
part0000_split_021.html
part0000_split_022.html
part0000_split_023.html
part0000_split_024.html
part0000_split_025.html
part0000_split_026.html
part0000_split_027.html
part0000_split_028.html
part0000_split_029.html
part0000_split_030.html
part0000_split_031.html
part0000_split_032.html
part0000_split_033.html
part0000_split_034.html
part0000_split_035.html
part0000_split_036.html
part0000_split_037.html
part0000_split_038.html
part0000_split_039.html
part0000_split_040.html
part0000_split_041.html
part0000_split_042.html
part0000_split_043.html
part0000_split_044.html
part0000_split_045.html
part0000_split_046.html
part0000_split_047.html
part0000_split_048.html
part0000_split_049.html
part0000_split_050.html
part0000_split_051.html
part0000_split_052.html
part0000_split_053.html
part0000_split_054.html
part0000_split_055.html
part0000_split_056.html
part0000_split_057.html
part0000_split_058.html
part0000_split_059.html
part0000_split_060.html
part0000_split_061.html
part0000_split_062.html
part0000_split_063.html
part0000_split_064.html
part0000_split_065.html
part0000_split_066.html
part0000_split_067.html
part0000_split_068.html
part0000_split_069.html
part0000_split_070.html
part0000_split_071.html
part0000_split_072.html
part0000_split_073.html
part0000_split_074.html
part0000_split_075.html
part0000_split_076.html
part0000_split_077.html
part0000_split_078.html
part0000_split_079.html
part0000_split_080.html
part0000_split_081.html
part0000_split_082.html
part0000_split_083.html
part0000_split_084.html
part0000_split_085.html
part0000_split_086.html
part0000_split_087.html
part0000_split_088.html
part0000_split_089.html
part0000_split_090.html
part0000_split_091.html
part0000_split_092.html
part0000_split_093.html
part0000_split_094.html
part0000_split_095.html
part0000_split_096.html
part0000_split_097.html
part0000_split_098.html
part0000_split_099.html
part0000_split_100.html
part0000_split_101.html
part0000_split_102.html
part0000_split_103.html
part0000_split_104.html
part0000_split_105.html
part0000_split_106.html
part0000_split_107.html
part0000_split_108.html
part0000_split_109.html
part0000_split_110.html
part0000_split_111.html
part0000_split_112.html
part0000_split_113.html
part0000_split_114.html
part0000_split_115.html
part0000_split_116.html
part0000_split_117.html
part0000_split_118.html
part0000_split_119.html
part0000_split_120.html
part0000_split_121.html
part0000_split_122.html
part0000_split_123.html
part0000_split_124.html
part0000_split_125.html
part0000_split_126.html
part0000_split_127.html
part0000_split_128.html
part0000_split_129.html
part0000_split_130.html
part0000_split_131.html
part0000_split_132.html
part0000_split_133.html
part0000_split_134.html
part0000_split_135.html
part0000_split_136.html
part0000_split_137.html
part0000_split_138.html
part0000_split_139.html