根據系統時間設置現在格式化日期時間

Calendar mDummyDate;
mDummyDate = Calendar.getInstance();
java.text.DateFormat shortDateFormat = DateFormat.getDateFormat(context);//注意這個要導入 import android.text.format.DateFormat; 這個包而不是 java下的包,倒錯會報錯的
final Calendar now = Calendar.getInstance();
mDummyDate.setTimeZone(now.getTimeZone());
mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0);
Date dummyDate = mDummyDate.getTime();
Log.i(TAG, "date format: " + shortDateFormat.format(dummyDate));
// if (format != null && !format.equals("")) {
SimpleDateFormat formatter = new SimpleDateFormat(shortDateFormat.format(date));
String mTimestampd = formatter.format(date);
String time = MessageUtils.formatDateStampString(context, date);
String showtime = mTimestampd + " " + time;
mTimestampHW = showtime;
Log.i(TAG, " mTimestampHW = "+mTimestampHW);
System.out.println(" date = " + date + " date+ mTimestamp = showtime==" + showtime + " mTimestampHW = " + mTimestampHW);
// } else {

// mTimestampHW = MessageUtils.formatDateStampString(context, date, true);
Log.i(TAG, " mTimestampHW = "+mTimestampHW);
//=====================================================================
//格式化時間
public static String formatDateStampString(Context context, long when) {
Time then = new Time();
then.set(when);
Time now = new Time();
now.setToNow();
// Basic settings for formatDateTime() we want for all cases.
int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT |
DateUtils.FORMAT_ABBREV_ALL |
DateUtils.FORMAT_CAP_AMPM;
format_flags |= DateUtils.FORMAT_SHOW_TIME;;
return DateUtils.formatDateTime(context, when, format_flags);
}
//格式化日期和時間
public static String formatTimeStampString(Context context, long when, boolean fullFormat) {
Time then = new Time();
then.set(when);
Time now = new Time();
now.setToNow();

// Basic settings for formatDateTime() we want for all cases.
int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT |
DateUtils.FORMAT_ABBREV_ALL |
DateUtils.FORMAT_CAP_AMPM;

// If the message is from a different year, show the date and year.
if (then.year != now.year) {
format_flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
} else if (then.yearDay != now.yearDay) {
// If it is from a different day than today, show only the date.
format_flags |= DateUtils.FORMAT_SHOW_DATE;
} else {
// Otherwise, if the message is from today, show the time.
format_flags |= DateUtils.FORMAT_SHOW_TIME;
}

// If the caller has asked for full details, make sure to show the date
// and time no matter what we've determined above (but still make showing
// the year only happen if it is a different year from today).
if (fullFormat) {
format_flags |= (DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);


}

return DateUtils.formatDateTime(context, when, format_flags);}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章