android中时间 和 日期 格式化显示

来源:互联网 发布:虚拟光驱 for mac 编辑:程序博客网 时间:2024/06/05 10:13
Android中时间 和 日期 格式化显示:
   public static String format24HourString(Context context, String time) {        boolean is24Hour = DateFormat.is24HourFormat(context);        Log.d("ljb", "time:" + time + ";is24Hour:" + is24Hour);        String[] timeStrings = time.split(":");        if (timeStrings.length < 2) {            return time;        }        if (!is24Hour) {            String hourString = timeStrings[0];            String minString = timeStrings[1];            int hour = Integer.parseInt(hourString);            if (hour > 12) {                hour = hour - 12;                return hour + ":" + minString + " "                        + context.getResources().getString(R.string.str_pm);            } else if (hour == 12) {                return hour + ":" + minString + " "                        + context.getResources().getString(R.string.str_pm);            } else {                return hour + ":" + minString + " "                        + context.getResources().getString(R.string.str_am);            }        } else {            return time;        }    }    public static String converHourFormat(Context context, String hour) {        boolean is24hFormart = DateFormat.is24HourFormat(context);        if (!is24hFormart) {            String AmOrPm = context.getResources().getString(R.string.str_am);            int hourTemp = Integer.parseInt(hour);            if (hourTemp > 12) {                AmOrPm = context.getResources().getString(R.string.str_pm);                hourTemp -= 12;            } else if (hourTemp == 12) {                AmOrPm = context.getResources().getString(R.string.str_pm);            } else {                AmOrPm = context.getResources().getString(R.string.str_am);            }            return AmOrPm + " " + hourTemp;        } else {            return hour;        }    }    public static String dateFormate(Context context, Time curTime) {        String dateformat = SystemProperties.get("persist.sys.dateformat", "1");        if ("0".equals(dateformat)) {            dateformat = "%m/%d/%Y";        } else if ("1".equals(dateformat)) {            dateformat = "%d/%m/%Y";        } else {            dateformat = "%Y/%m/%d";        }        return curTime.format(dateformat);    }    public static String dateFormate(Context context, Date curDate) {        String dateformat = SystemProperties.get("persist.sys.dateformat", "1");        if ("0".equals(dateformat)) {            dateformat = "MM-dd-yyyy";        } else if ("1".equals(dateformat)) {            dateformat = "dd-MM-yyyy";        } else {            dateformat = "yyyy-MM-dd";        }        return DateFormat.format(dateformat, curDate).toString();    }

原创粉丝点击