CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Dates

Dates and Strings
Dates and times
DateFormat

The DateFormat class

java.text.DateFormat;

Common static methods

Method Description
getDateInstance() Returns a DateFormat object with date, but not time.
getTimeInstance() Returns a DateFormat object with time, but not date.
getDateTimeInstance() Returns a DateFormat object with date and time.
getDateInstance(intField)
getTimeInstance(intField)
getDateTimeInstance( intField, intField)
Same as above, but you can use the fields shown next to override the default date or time format.

Common fields of the DateFormat class

Style Date example Time example
SHORT 12/31/05 12:00 AM
MEDIUM Dec 31, 2005 7:30:00 PM
LONG December 31, 2005 7:30:00 AM PST
FULL Saturday, December 31, 2005 7:30:00 AM PST

Common method

Method Description
format(Date) Returns a String object of the Date object with the format that's specified by the DateFormat object.

Code that formats a Date object

Date now = new Date();
DateFormat defaultDate = DateFormat.getDateTimeInstance();
String nowString = defaultDate.format(now);

Code that formats a GregorianCalendar object

GregorianCalendar gregEndDate = new GregorianCalendar(2005,11,31,7,30);
Date endDate = gregEndDate.getTime();
DateFormat defaultDate = DateFormat.getDateInstance();
String endDateString = defaultDate.format(endDate);

Code that overrides the default date and time formats

DateFormat shortDate = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat shortTime = DateFormat.getTimeInstance(DateFormat.SHORT);
DateFormat shortDateTime = DateFormat.getDateTimeInstance
                           		(DateFormat.SHORT, DateFormat.SHORT);

The SimpleDateFormat class

java.text.SimpleDateFormat;

If we want to display the moth, day, and year in the MM/dd/yy shorthand format, such as 07/20/11, we write

Date today;
SimpledateFormat sdf;
today = new Date();
sdf = new SimpleDateFormat("MM/dd/yy");
system.out.println(sdf.format());

Symbol Meaning Value Sample
y Year Number yyyy -> 2011
M Month in year text or number MM -> 07
MMM -> Jul
MMMM -> July
d Day in month Number dd -> 20
D Day in year Number DDD -> 201
h Hour in AM/PM Number hh -> 12
H Hour in day (0-23) Number HH -> 00
a AM/PM marker Text a -> AM
m Minutes in hour Number mm -> 01
s Seconds in minute Number ss -> 06
S Millisecond Number mmm -> 026
E Day in week Text E -> Wed
EEEE -> Wednesday

import java.util.*;	// for Date
import java.text.*;	// for SimpleDateFormat

public class DateDisplay
{

	public static void main(String[] args)
	{
		Date today;
		SimpleDateFormat simpleDF1, simpleDF2;
		today = new Date();
		simpleDF1 = new SimpleDateFormat();
		simpleDF2 = new SimpleDateFormat("EEEE MMMM dd, yyyy");
		// Default short format display
		System.out.println("Today is " + simpleDF1.format(today));

		// programmer-designated long format display
		System.out.println("Today is " + simpleDF2.format(today));

	}
}

Previous | GregorianCalendar | Calendar | Date | DateFormat | DateUtils | Invoice | Next