日期时间解析类 ,该类位于android.text.format.DateFormat这个package中,该类提供了Java中的三种时间对象,提示大家下面三种方法为静态可以直接调用,如下:
final static CharSequence format(CharSequence inFormat, Date inDate) //传入Date对象
Given a format string and a Date object, returns a CharSequence containing the requested date.
final static CharSequence format(CharSequence inFormat, Calendar inDate) //Calendar对象
Given a format string and a Calendar object, returns a CharSequence containing the requested date.
final static CharSequence format(CharSequence inFormat, long inTimeInMillis) //long对象
Given a format string and a time in milliseconds since Jan 1, 1970 GMT, returns a CharSequence containing the requested date.
我们可能看到了第一个参数均为inFormat这是一个CharSequence接口的String类型,它提供了灵活的时间格式解析字符串描述,提示大家注意大小写要区分,如
April 6, 1970 at 3:23am 例子,那么inFormat参数的写法和最终执行的结果如下对照,
下面就以Android123的CWJ生日为例子如下
"MM/dd/yy h:mmaa" -> "11/03/87 11:23am"
"MMM dd, yyyy h:mmaa" -> "Nov 3, 1987 11:23am"
"MMMM dd, yyyy h:mmaa" -> "November 3, 1987 11:23am"
"E, MMMM dd, yyyy h:mmaa" -> "Tues , November 3, 1987 11:23am"
"EEEE, MMMM dd, yyyy h:mmaa" -> "Tues day, Nov 3, 1987 11:23am"
用24小时制:
"EEEE, MMMM dd, yyyy kk:mm" -> "Tues day, Nov 3, 1987 23:23"
其中:12小时制 :hh; 24小时制: kk
如果用 SimpleDateFormat ,则为: hh; HH
android.text.format.DateFormat类的static boolean is24HourFormat(Context context)方法可以用来判断当前系统时间是否为24小时制式
Constants
public static final char AM_PM
This designator indicates whether the HOUR
field is before or after noon. The output is lower-case. Examples: a -> a or p aa -> am or pm
public static final char CAPITAL_AM_PM
This designator indicates whether the HOUR
field is before or after noon. The output is capitalized. Examples: A -> A or P AA -> AM or PM
public static final char DATE
This designator indicates the day of the month. Examples for the 9th of the month: d -> 9 dd -> 09
public static final char DAY
This designator indicates the name of the day of the week. Examples for Sunday: E -> Sun EEEE -> Sunday
public static final char HOUR
This designator indicates the hour of the day in 12 hour format. Examples for 3pm: h -> 3 hh -> 03
public static final char HOUR_OF_DAY
This designator indicates the hour of the day in 24 hour format. Example for 3pm: k -> 15 Examples for midnight: k -> 0 kk -> 00
public static final char MINUTE
This designator indicates the minute of the hour. Examples for 7 minutes past the hour: m -> 7 mm -> 07
public static final char MONTH
This designator indicates the month of the year Examples for September: M -> 9 MM -> 09 MMM -> Sep MMMM -> September
public static final char QUOTE
Text in the format string that should be copied verbatim rather that interpreted as formatting codes must be surrounded by the QUOTE
character. If you need to embed a literal QUOTE
character in the output text then use two in a row.
public static final char SECONDS
This designator indicates the seconds of the minute. Examples for 7 seconds past the minute: s -> 7 ss -> 07
public static final char TIME_ZONE
This designator indicates the offset of the timezone from GMT. Example for US/Pacific timezone: z -> -0800 zz -> PST
public static final char YEAR
This designator indicates the year. Examples for 2006 y -> 06 yyyy -> 2006