2011年6月5日日曜日

Android 日時の表示形式を指定する

DateUtils.formatDateTime (Context context, long millis, int flags)

DateUtils.formatDateRange (Context context, Formatter formatter, long startMillis, long endMillis, int flags, String timeZone)

DateUtils.formatDateRange (Context context, long startMillis, long endMillis, int flags)

DateUtils.formatDateRange (Context context, Formatter formatter, long startMillis, long endMillis, int flags)

の flag に指定するパラメータ
| で組み合わせる


■表示する要素を指定

 DateUtils.FORMAT_SHOW_TIME 時刻を表示
 DateUtils.FORMAT_SHOW_DATE 日付を表示
 DateUtils.FORMAT_SHOW_WEEKDAY 曜日を表示
 DateUtils.FORMAT_SHOW_YEAR 年を表示
 DateUtils.FORMAT_NUMERIC_DATE 
 DateUtils.FORMAT_UTC

■時刻に関するフォーマット

 DateUtils.FORMAT_12HOUR
 DateUtils.FORMAT_24HOUR
 DateUilts.FORMAT_CAP_AMPM
 DateUilts.FORMAT_CAP_MIDNIGHT
 DateUilts.FORMAT_CAP_NOON
 DateUilts.FORMAT_CAP_NONN_MIDNIGHT
 DateUilts.FORMAT_ABBREV_TIME
 DateUilts.FORMAT_NO_NOON
 DateUilts.FORMAT_NO_MIDNIGHT
 DateUilts.FORMAT_NO_NOON_MIDNIGHT

■日付に関するフォーマット

 DateUilts.FORMAT_ABBREV_MONTH

■曜日に関するフォーマット

 DateUtils.FORMAT_ABBREV_WEEKDAY



int[] TIME_FLAGS = {
DateUtils.FORMAT_SHOW_TIME,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_AMPM,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_MIDNIGHT,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_NOON,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_NOON_MIDNIGHT,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_TIME,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_NOON,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_MIDNIGHT,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_NOON_MIDNIGHT,
};

int[] DATE_FLAGS = {
DateUtils.FORMAT_SHOW_DATE,
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH,
DateUtils.FORMAT_NUMERIC_DATE,
DateUtils.FORMAT_UTC,
};

int[] WEEKDAY_FLAGS = {
DateUtils.FORMAT_SHOW_WEEKDAY,
DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY,
};

int[] COMBINE_FLAGS = {
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_WEEKDAY,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR |
DateUtils.FORMAT_ABBREV_ALL,
};


英語 Location

日本語 Location



コード全部はこんな感じ

package yanzm.example.sampletest;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SampleTestActivity extends Activity {
private static Time sTime = new Time();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

int[] TIME_FLAGS = {
DateUtils.FORMAT_SHOW_TIME,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_AMPM,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_MIDNIGHT,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_NOON,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_NOON_MIDNIGHT,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_TIME,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_NOON,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_MIDNIGHT,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_NOON_MIDNIGHT,
};

int[] DATE_FLAGS = {
DateUtils.FORMAT_SHOW_DATE,
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH,
DateUtils.FORMAT_NUMERIC_DATE,
DateUtils.FORMAT_UTC,
};

int[] WEEKDAY_FLAGS = {
DateUtils.FORMAT_SHOW_WEEKDAY,
DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY,
};

int[] COMBINE_FLAGS = {
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR,
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_ALL,
};

long TIME1 = parseTime("2011-07-17T14:00:00.000+09:00");
long TIME2 = parseTime("2011-07-17T12:00:00.000+09:00");
long TIME3 = parseTime("2011-07-17T00:00:00.000+09:00");

for(int i = 0; i < TIME_FLAGS.length; i++){
TableRow tr = new TableRow(this);
TextView tv;
tv = new TextView(this);
tv.setPadding(0, 0, 40, 0);
tv.setText(DateUtils.formatDateTime(this, TIME1, TIME_FLAGS[i]));
tr.addView(tv);
tv = new TextView(this);
tv.setText(DateUtils.formatDateTime(this, TIME2, TIME_FLAGS[i]));
tr.addView(tv);
tv = new TextView(this);
tv.setPadding(40, 0, 0, 0);
tv.setText(DateUtils.formatDateTime(this, TIME3, TIME_FLAGS[i]));
tr.addView(tv);

TableLayout tl = (TableLayout)findViewById(R.id.time);
tl.addView(tr);
}

long DATE1 = parseTime("2011-07-17T14:00:00.000+09:00");
long DATE2 = parseTime("2011-10-05T14:00:00.000+09:00");

for(int i = 0; i < DATE_FLAGS.length; i++){
TableRow tr = new TableRow(this);
TextView tv;
tv = new TextView(this);
tv.setPadding(0, 0, 40, 0);
tv.setText(DateUtils.formatDateTime(this, DATE1, DATE_FLAGS[i]));
tr.addView(tv);
tv = new TextView(this);
tv.setText(DateUtils.formatDateTime(this, DATE2, DATE_FLAGS[i]));
tr.addView(tv);

TableLayout tl = (TableLayout)findViewById(R.id.date);
tl.addView(tr);
}

for(int i = 0; i < WEEKDAY_FLAGS.length; i++){
TableRow tr = new TableRow(this);
TextView tv;
tv = new TextView(this);
tv.setPadding(0, 0, 40, 0);
tv.setText(DateUtils.formatDateTime(this, DATE1, WEEKDAY_FLAGS[i]));
tr.addView(tv);
tv = new TextView(this);
tv.setText(DateUtils.formatDateTime(this, DATE2, WEEKDAY_FLAGS[i]));
tr.addView(tv);

TableLayout tl = (TableLayout)findViewById(R.id.weekday);
tl.addView(tr);
}

for(int i = 0; i < COMBINE_FLAGS.length; i++){
TableRow tr = new TableRow(this);
TextView tv;
tv = new TextView(this);
tv.setText(DateUtils.formatDateTime(this, DATE1, COMBINE_FLAGS[i]));
tr.addView(tv);

TableLayout tl = (TableLayout)findViewById(R.id.combine);
tl.addView(tr);
}
}

public static long parseTime(String time) {
sTime.parse3339(time);
return sTime.toMillis(false);
}
}

0 件のコメント:

コメントを投稿