【Android】 Android中对于时间的转换
16lz
2021-12-04
/*
* 将String转成Date类型
* 将GMT时间转换成当前时区时间
*/
public static String transform(String from){
String to = "";
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//本地时区
Calendar nowCal = Calendar.getInstance();
TimeZone localZone = nowCal.getTimeZone();
//设定SDF的时区为本地
simple.setTimeZone(localZone);
SimpleDateFormat simple1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//设置 DateFormat的时间区域为GMT
simple1.setTimeZone(TimeZone.getTimeZone("GMT"));
//把字符串转化为Date对象,然后格式化输出这个Date
Date fromDate = new Date();
try {
//时间string解析成GMT时间
fromDate = simple1.parse(from);
//GMT时间转成当前时区的时间
to = simple.format(fromDate);
} catch (ParseException e1) {
e1.printStackTrace();
}
return to;
}
* 将String转成Date类型
* 将GMT时间转换成当前时区时间
*/
public static String transform(String from){
String to = "";
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//本地时区
Calendar nowCal = Calendar.getInstance();
TimeZone localZone = nowCal.getTimeZone();
//设定SDF的时区为本地
simple.setTimeZone(localZone);
SimpleDateFormat simple1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//设置 DateFormat的时间区域为GMT
simple1.setTimeZone(TimeZone.getTimeZone("GMT"));
//把字符串转化为Date对象,然后格式化输出这个Date
Date fromDate = new Date();
try {
//时间string解析成GMT时间
fromDate = simple1.parse(from);
//GMT时间转成当前时区的时间
to = simple.format(fromDate);
} catch (ParseException e1) {
e1.printStackTrace();
}
return to;
}
更多相关文章
- android 设置系统屏幕亮度
- android 查看 当前activity
- android:exported 属性详解
- Android(安卓)定时任务的多种实现方式
- Android——获取手机当前信号强度(dbm/asu值)
- Android(安卓)ApiDemos示例解析(15):App->Activity->SetWallpaper
- Android(安卓)设备+APP+号码信息
- Android(安卓)中文API (32) ―― TimePicker.OnTimeChangedListene
- android与j2me移植之clipRect