Predefined date formats

Java DateFormat class provides predefined ways to parse and format dates for any locale. This example demonstrates the defaults provided.

Default

DateFormat.DEFAULT should result in a format of Oct 21, 2013.

@Test
public void format_date_predefined_default () {

    DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT);

    Date today = new Date();

    logger.info(dateFormatter.format(today));

    assertThat(dateFormatter.format(today), containsString(","));
}

Output

Oct 21, 2013

Short

DateFormat.SHORT should result in a format of 10/21/13.

@Test
public void format_date_predefined_short () {

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

    Date today = new Date();

    logger.info(dateFormatter.format(today));

    assertThat(dateFormatter.format(today), containsString("/"));
}

Output

10/21/13

Medium

DateFormat.MEDIUM should result in a format of Oct 21, 2013.

@Test
public void format_date_predefined_medium () {

    DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM);

    Date today = new Date();

    logger.info(dateFormatter.format(today));

    assertThat(dateFormatter.format(today), containsString(","));
}

Output

Oct 21, 2013

Long

DateFormat.LONG should result in a format of October 21, 2013.

@Test
public void format_date_predefined_long () {

    DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.LONG);

    Date today = new Date();

    logger.info(dateFormatter.format(today));

    assertThat(dateFormatter.format(today), containsString(","));
}

Output

October 21, 2013

Full

DateFormat.LONG should result in a format of Monday, October 21, 2013.

@Test
public void format_date_predefined_full () {

    DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.FULL);

    Date today = new Date();

    logger.info(dateFormatter.format(today));

    assertThat(dateFormatter.format(today), containsString(","));
}

Output

Monday, October 21, 2013