日期时间解析类

日期时间解析类

    该类位于Android.text.format.DateFormat这个package中,该类提供了Java中的三种时间对象,提示大家下面三种方法为静态可以直接调用,如下:  
1.  final static CharSequence  format(CharSequence inFormat, Date inDate)  //传入Date对象
        Given a format string and a Date object, returns a CharSequence containing the requested date.

2.  final static CharSequence  format(CharSequence inFormat, Calendar inDate)  //Calendar对象
        Given a format string and a Calendar object, returns a CharSequence containing the requested date.
3.  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类型,它提供了灵活的时间格式解析字符串描述,(注意大小写要区分)

    下面就生日为例子如下
    "MM/dd/yy h:mmaa" -> "11/03/99 20:06am"
    "MMM dd, yyyy h:mmaa" -> "Nov 3, 1999 20:06am"
    "MMMM dd, yyyy h:mmaa" -> "November  3, 1999 20:06am"
    "E, MMMM dd, yyyy h:mmaa" -> "Tues , November 3, 1999 20:06am"
    "EEEE, MMMM dd, yyyy h:mmaa" -> "Tues day, Nov 3, 1999 20:06am"

    用24小时制:

    "EEEE, MMMM dd, yyyy kk:mm" -> "Tues day, Nov 3, 1987 23:23"

其中:12小时制 :hh; 24小时制: kk

SimpleDateFormat ,则为: hh; HH
其中判断当前系统时间是否为24小时制式:

android.text.format.DateFormat类的static boolean is24HourFormat(Context context)

Demo: 获取是否是24小时,如果是24小时,显示举例为13:09;若是12进制,则显示举例为2:01pm:

boolean is24HourFormat = DateFormat.is24HourFormat(context);
String format =is24HourFormat ?"kk:mm":"hh:mmaa";
String result =  DateFormat.format(format,System.currentTimeMillis()).toString();

Constants:

    public static final char AM_PM
    Since: API Level 3

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

    Constant Value: 97 (0x00000061)
    public static final char CAPITAL_AM_PM
    Since: API Level 3

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

    Constant Value: 65 (0x00000041)
    public static final char DATE
    Since: API Level 3

This designator indicates the day of the month. Examples for the 9th of the month: d -> 9 dd -> 09

    Constant Value: 100 (0x00000064)
    public static final char DAY
    Since: API Level 3

This designator indicates the name of the day of the week. Examples for Sunday: E -> Sun EEEE -> Sunday

    Constant Value: 69 (0x00000045)
    public static final char HOUR
    Since: API Level 3

This designator indicates the hour of the day in 12 hour format. Examples for 3pm: h -> 3 hh -> 03

    Constant Value: 104 (0x00000068)
    public static final char HOUR_OF_DAY
    Since: API Level 3

This designator indicates the hour of the day in 24 hour format. Example for 3pm: k -> 15 Examples for midnight: k -> 0 kk -> 00

    Constant Value: 107 (0x0000006b)
    public static final char MINUTE
    Since: API Level 3

This designator indicates the minute of the hour. Examples for 7 minutes past the hour: m -> 7 mm -> 07

    Constant Value: 109 (0x0000006d)
    public static final char MONTH
    Since: API Level 3

This designator indicates the month of the year Examples for September: M -> 9 MM -> 09 MMM -> Sep MMMM -> September

    Constant Value: 77 (0x0000004d)
    public static final char QUOTE
    Since: API Level 3

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.

    Constant Value: 39 (0x00000027)
    public static final char SECONDS    
    Since: API Level 3

This designator indicates the seconds of the minute. Examples for 7 seconds past the minute: s -> 7 ss -> 07

    Constant Value: 115 (0x00000073)
    public static final char TIME_ZONE  
    Since: API Level 3

This designator indicates the offset of the timezone from GMT. Example for US/Pacific timezone: z -> -0800 zz -> PST

    Constant Value: 122 (0x0000007a)
    public static final char YEAR       
    Since: API Level 3

This designator indicates the year. Examples for 2006 y -> 06 yyyy -> 2006

你可能感兴趣的:(Android,开发分享)