Skip to content

how to use android.Format to format date! #2

@IamANYWHERE

Description

@IamANYWHERE

this is a issue for learning android.
When I need some format time,Format class gives us three overloading static method which is format to
make date properly.

public static CharSequence format (CharSequence inFormat, Calendar inDate)
Given a format string and a Calendar object, returns a CharSequence containing the requested date.
public static CharSequence format (CharSequence inFormat, Date inDate)
Given a format string and a Date object, returns a CharSequence containing the requested date.
public static CharSequence format (CharSequence inFormat, long inTimeInMillis)
Given a format string and a time in milliseconds since Jan 1, 1970 GMT, returns a CharSequence containing the requested date.

this is my code in android
String date;
date=DateFormat.format("yyyy年mm月dd日,hh:mm",mCrime.getDate()).toString();
mDateButton.setText(date);

this is the code to view
image

additionally,we can define format on our own.
like this

"MM/dd/yy h:mmaa"
"MMM DD,YYY h:mmaa"
"mmmm dd,yyyy h:mmaa"
"e,mmmm dd,yyyy h:mmaa"
"eeee,mmmm dd,yyyy h:mmaa"
"yyyy年mm月dd日,kk:mm"

this is the table

字母 含义 示例
y Year 1996; 96 哪一年
M Month in year J uly;Jul; 07 一年中的哪一月
m Minute in hour 30 一个小时中的第几分钟
D Day in year 189 一年中的第几天
d Day in month 2 10 一个月中的第几天
H Hour in day ( 0- 23) 0 一天中的第几个小时( 24小时制)
h Hour in am/pm ( 1- 12) 12 一天中上午、下午的第几个小时( 12小时制)
S Millisecond 978 毫秒数
s Second in minute 55 一分钟的第几秒

the time I print is not the same as realtime.
I search for it,but I still can't find a way to solve it.
So ,I need more time to get the answer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions