/**
* 当地时间 ---> UTC时间
* @return
*/
public static String Local2UTC(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = sdf.format(new Date());
return gmtTime;
}
/**
* UTC时间 ---> 当地时间
* @param utcTime UTC时间
* @return
*/
public static String utc2Local(String utcTime) {
SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC时间格式
utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
Date gpsUTCDate = null;
try {
gpsUTCDate = utcFormater.parse(utcTime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//当地时间格式
localFormater.setTimeZone(TimeZone.getDefault());
String localTime = localFormater.format(gpsUTCDate.getTime());
return localTime;
}android中 utc 和 当地时间的转换
最新推荐文章于 2024-09-27 18:52:11 发布
本文提供了两种时间转换的方法:一种是从本地时间转换为UTC时间,另一种是从UTC时间转换为本地时间。这两种方法都使用了Java的SimpleDateFormat类来格式化日期,并通过设置不同的时区来实现时间的转换。

1306

被折叠的 条评论
为什么被折叠?



