android日期时间格式转换:

private static int flagsDate = DateUtils.FORMAT_SHOW_DATE;private static int flagsTime = DateUtils.FORMAT_SHOW_TIME ;private static int flagsWeek = DateUtils.FORMAT_SHOW_WEEKDAY;String dateStr = (String)DateUtils.formatDateTime(context, System.currentTimeMillis(), flagsDate);//5月11日String timeStr = (String)DateUtils.formatDateTime(context, System.currentTimeMillis(), flagsTime);//16:40String weekStr = (String)DateUtils.formatDateTime(context, System.currentTimeMillis(), flagsWeek);//星期二

12小时格式时,获取上午还是下午:

String smPmStr = DateUtils.getAMPMString(Calendar.getInstance().get(Calendar.AM_PM));//上午(下午)

12小时时间格式时,只显示时间,不显示“上午“这样的字符串:

private final static String M12 = "h:mm";private final static String M24 = "kk:mm";formatTime = android.text.format.DateFormat.is24HourFormat(context)? M24 : M12;String timeStr = (String) DateFormat.format(formatTime,System.currentTimeMillis());

将系统当前事件,转化为所需格式:

long dateTaken = System.currentTimeMillis();if (dateTaken != 0) {        String datetime = DateFormat.format("yyyy:MM:dd kk:mm:ss", dateTaken).toString();        Log.e(TAG,"datetime : " +  datetime);                }

如果为今天,则显示时间,如果不是今天则显示日期:

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_DATE;date = (String)DateUtils.formatDateTime(mContext, dateTime, flags);}        return date;    }

在源码短信息模块中MessageUtils.java中有这样一个函数,与上面的功能相同:

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://blog.csdn.net/dany1202/article/details/6164322

更多相关文章

  1. Android修炼之道—时间测量
  2. android 使用Okhttp封装上传JSON格式数据的工具类
  3. android:获取网络时间、文件路径,修改包名,发布release版本
  4. Android API等级、Android版本、发布日期 《一览表》
  5. 日期和时间(DatePicker、TimePicker)
  6. Android获取当前时间
  7. 日期选择控件
  8. android默认系统日期、时间、时区更改
  9. Android--MediaPlayer音乐播放器歌词随时间高亮显示

随机推荐

  1. Eclipse 重装Android(安卓)ADT 问题~解决
  2. android 渗透测试必备工具
  3. Android(安卓)Studio 2.2 预览 - 新的UI
  4. android的消息处理机制(图+源码分析)——Lo
  5. android全格式多媒体播放器(一:ffmpeg移植)
  6. Android中自定义PopupWindow,动态弹窗。
  7. Android实现静默安装与卸载
  8. android之view获取getWidth()和getHeight
  9. android开发笔记1:TextView的属性详解
  10. Android中内存优化