您的位置:首页 > 其它

long型的时间转换为时间格式 短信模块用到

2013-01-16 10:12 232 查看
//如果为今天,则显示时间(如2013年1月16日),如果不是今天则显示日期(今天10:15)

private String getDate(long dateTime)

{

int flags = 0;

String date = "";

if (DateUtils.isToday(dateTime))

{

flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR;

date = "今天"+(String)DateUtils.formatDateTime(mContext, dateTime, flags);

}

else

{

flags = DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;

date = (String)DateUtils.formatDateTime(mContext, dateTime, flags);

}

return date;

}

方法二:/如果不是今年,则显示时间(如2012-1-16),是是今年非今天则显示月日1月15日,如果是今天则显示日期(上午10:15)

public static String formatTimeStampString(Context context, long when) {

return formatTimeStampString(context, when, false);

}

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);

}

参考http://www.linuxidc.com/Linux/2012-08/68510.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: