根据系统时间设置现在格式化日期时间

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);}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值