This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging
background
The previous article used SimpleDateFormat for formatting times, but sometimes (such as different formats depending on language or time) it’s not sufficient for all requirements. This article focuses on how to use DateFormat
DateFormat
The DateFormat class is available in three packages: java.text,android.text.format,and Android.icu. Text. The DateFormat class in Android.icu And, mainly write DateFormat class in java.text and Android.text. format
java.text.DateFormat
The main method getDateTimeInstance returns the corresponding format string by passing in the specified style and is automatically translated according to the language
Common style constants
Dateformat. FULL Prints the FULL datetime dateFormat. LONG prints the LONG style datetime dateformat. MEDIUM prints the MEDIUM style datetime, DEFAULT style dateFormat. SHORT prints the date and time of the SHORT style dateformat. DEFAULT Prints the date and time of the DEFAULT style \
Formatted date
code
Date date = new Date(System.currentTimeMillis());
System.out.print("DateFormat.FULL -- ");
System.out.println(DateFormat.getDateInstance(DateFormat.FULL).format(date));
System.out.print("DateFormat.LONG -- ");
System.out.println(DateFormat.getDateInstance(DateFormat.LONG).format(date));
System.out.print("DateFormat.MEDIUM -- ");
System.out.println(DateFormat.getDateInstance(DateFormat.MEDIUM).format(date));
System.out.print("DateFormat.SHORT -- ");
System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(date));
System.out.print("DateFormat.DEFAULT -- ");
System.out.println(DateFormat.getDateInstance(DateFormat.DEFAULT).format(date));
Copy the code
The effect
Format the date + time
Date date = new Date(System.currentTimeMillis());
System.out.print("DateFormat.FULL -- ");
System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL).format(date));
System.out.print("DateFormat.LONG -- ");
System.out.println(DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).format(date));
System.out.print("DateFormat.MEDIUM -- ");
System.out.println(DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM).format(date));
System.out.print("DateFormat.SHORT -- ");
System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT).format(date));
System.out.print("DateFormat.DEFAULT -- ");
System.out.println(DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT).format(date));
Copy the code
android.text.DateFormat
From a code point of view, Android.text.dateFormat is basically a wrapper and extension of the java.text.dateFormat class
Common methods
format(CharSequence inFormat, GetDateFormat: return a DateFormat object. GetDateFormat: return a DateFormat object. GetLongDateFormat: Returns a DateFormat object that formats the date in a long format (for example, Monday, January 3, 2000) for the context’s locale. GetMediumDateFormat returns a DateFormat object that can format the date in a medium form (for example, January 3, 2000) for the locale of the context. GetTimeFormat returns a DateFormat object that can set the time format based on the locale of the context and the user’s 12-hour / 24-hour preferences. Is24HourFormat Returns true if the time should be formatted as a 24-hour time, and false if the time should be formatted as a 12-hour (AM/PM) time.