Mega Code Archive

 
Categories / Java Book / 003 Essential Classes
 

0206 DataFormat class

DateFormat is an abstract class for date/time formatting. DateFormat is an abstract class that provides the ability to format and parse dates and times. The getDateInstance( ) method returns an instance of DateFormat that can format date information. It is available in these forms: static final DateFormat getDateInstance( ) static final DateFormat getDateInstance(int style) static final DateFormat getDateInstance(int style, Locale locale) The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used. format( ) method has several overloaded forms, one of which is shown here: final String format(Date d) The argument is a Date object that is to be displayed. The method returns a string containing the formatted information. // Demonstrate date formats. import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Main { public static void main(String args[]) { Date date = new Date(); DateFormat df; df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA); System.out.println("Korea: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date)); df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); System.out.println("United States: " + df.format(date)); } } The getTimeInstance( ) method returns an instance of DateFormat that can format time information. It is available in these versions: static final DateFormat getTimeInstance( ) static final DateFormat getTimeInstance(int style) static final DateFormat getTimeInstance(int style, Locale locale) The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used. Useful contants static int AM_PM_FIELD Useful constant for AM_PM field alignment. static int DATE_FIELD Useful constant for DATE field alignment. static int DAY_OF_WEEK_FIELD Useful constant for DAY_OF_WEEK field alignment. static int DAY_OF_WEEK_IN_MONTH_FIELD Useful constant for DAY_OF_WEEK_IN_MONTH field alignment. static int DAY_OF_YEAR_FIELD Useful constant for DAY_OF_YEAR field alignment. static int ERA_FIELD Useful constant for ERA field alignment. static int HOUR_OF_DAY0_FIELD Useful constant for zero-based HOUR_OF_DAY field alignment. static int HOUR_OF_DAY1_FIELD Useful constant for one-based HOUR_OF_DAY field alignment. static int HOUR0_FIELD Useful constant for zero-based HOUR field alignment. static int HOUR1_FIELD Useful constant for one-based HOUR field alignment. static int MILLISECOND_FIELD Useful constant for MILLISECOND field alignment. static int MINUTE_FIELD Useful constant for MINUTE field alignment. static int MONTH_FIELD Useful constant for MONTH field alignment. static int SECOND_FIELD Useful constant for SECOND field alignment. static int TIMEZONE_FIELD Useful constant for TIMEZONE field alignment. static int WEEK_OF_MONTH_FIELD Useful constant for WEEK_OF_MONTH field alignment. static int WEEK_OF_YEAR_FIELD Useful constant for WEEK_OF_YEAR field alignment. static int YEAR_FIELD Useful constant for YEAR field alignment. static int DEFAULT Constant for default style pattern. static int FULL Constant for full style pattern. static int LONG Constant for long style pattern. static int MEDIUM Constant for medium style pattern. static int SHORT Constant for short style pattern.