android获取系统标准时区的时间
如下是参考android系统源码获取统标准时区的时间,包括上下午的字串,例如设置系统日期格式为31-12-2012 ,那么返回的字串就是31-12-2012上午
主要是同步系统日期的格式,并且了解android 中SimpeDataFormat 和 Data 的用法。
[java]
/**
* 获得标准系统时间
* @param context
* @return
*/
public static String getDateAndTimeString(Context context) {
ContentResolver cv = context.getContentResolver();
Resources resources = context.getResources();
String timeString = null; www.zzzyk.com
String strDateFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.DATE_FORMAT);
if (TextUtils.isEmpty(strDateFormat)) {
strDateFormat = "yyyy-MM-dd";
}
SimpleDateFormat mmdateFormat = new SimpleDateFormat(strDateFormat);
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
long dateTaken = System.currentTimeMillis();
int time[] = getHourAndMinuteOfDay(new Date(dateTaken), strTimeFormat);
timeString = resources.getString(Res.string.taken_on) + ": "
+ mmdateFormat.format(new Date(dateTaken)) + " " + time[ID_HOUR] + ":"
+ (time[ID_MINUTE] < 10 ? ("0" + time[ID_MINUTE]) : time[ID_MINUTE]);
if (time[ID_AM_PM] != -1) {
timeString = timeString.toString() + DateUtils.getAMPMString(time[ID_AM_PM]);
}
return timeString;
}
/**
* 获得标准系统时间
* @param context
* @return
*/
public static String getDateAndTimeString(Context context) {
ContentResolver cv = context.getContentResolver();
Resources resources = context.getResources();
String timeString = null;
String strDateFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.DATE_FORMAT);
if (TextUtils.isEmpty(strDateFormat)) {
strDateFormat = "yyyy-MM-dd";
}
SimpleDateFormat mmdateFormat = new SimpleDateFormat(strDateFormat);
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
long dateTaken = System.currentTimeMillis();
int time[] = getHourAndMinuteOfDay(new Date(dateTaken), strTimeFormat);
timeString = resources.getString(Res.string.taken_on) + ": "
+ mmdateFormat.format(new Date(dateTaken)) + " " + time[ID_HOUR] + ":"
+ (time[ID_MINUTE] < 10 ? ("0" + time[ID_MINUTE]) : time[ID_MINUTE]);
if (time[ID_AM_PM] != -1) {
timeString = timeString.toString() + DateUtils.getAMPMString(time[ID_AM_PM]);
}
return timeString;
}getHourAndMinuteOfDay(new Date(dateTaken), strTimeFormat)中的dateTaken可以是任意时间,所以此方法稍加改动,重用性还是很强的。
补充:移动开发 , Android ,