The associated package for DateFormat:
-
java.text.DateFormat
Common static methods:
Method | Description |
getDateInstance() | Gets the date formatter with the default formatting style for the default locale |
getTimeInstance() | Gets the time formatter with the default formatting style for the default locale |
getDateTimeInstance() | Gets the date/time formatter with the default formatting style for the default locale |
getDateInstance(intField) | Gets the date formatter with the given formatting style for the default locale |
getTimeInstance(intField) | Gets the time formatter with the given formatting style for the default locale |
getDateTimeInstance(intField,intField) | Gets the date/time formatter with the given date and time formatting styles for the default locale. |
Common Enumberators of the DateFormat classes:
Style | Date Example | Time example |
SHORT | 12/31/05 | 12:00 AM |
MEDIUM | Dec 31, 2005 | 12:00:00 AM |
LONG | December 31, 2005 | 12:00:00 AM PST |
FULL | Saturday, December 31, 2005 | 12:00:00 AM PST |
Common mehtods of the DateFormat class:
Method | Description |
format(Date) | Returns a String object of Date object with the format that's specified by the DateFormat object. |
Descriptions:
-
You can use the DateFormat class to format Date objects in varius ways.
-
For more control use SimpleDateFormat located here: Using Simple Date Format to get GMT
Example 1: Code that formats a Date object
Davte now = new Date() DateFormat defaultDate = DateFormat.getDateTimeInstance(); String newString = defaultDate(now);
Example 2: 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);
Example 3: Code that overrides the default data and time formats
DateFormat shortDate = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat shorttIME = DateFormat.getTimeInstance(DateFormat.SHORT); DateFormat shortDateTime = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
For more information, see: