public static void main(String[] args) throws ParseException {
String utcTime = "2018-01-31T14:32:19Zsdfsdfsssss";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
//设置时区UTC
df.setTimeZone(TimeZone.getTimeZone("UTC"));
//格式化,转当地时区时间
Date after = df.parse(utcTime);
System.out.println(after);
}
utcTime Z后边有很多非法字符,解析竟然完全没有错误,是为什么呢?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- Lxinccode 2019-01-24 15:07关注
/** * 本地时间转 UTC 时间字符串 * * @param date * @return */ public static String localToUtcString(Date date, String pattern) { SimpleDateFormat sdf = getSdf(pattern); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); return sdf.format(date); } /** * UTC 时间反格式化 * * @param date * @param pattern * @return */ public static Date utcStringToUtcDate(String date, String pattern) { SimpleDateFormat sdf = getSdf(pattern); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date utcDate = null; try { utcDate = sdf.parse(date); } catch (Exception e) { } return utcDate; } /** * UTC 时间格式化 * * @param date * @return */ public static String utcDateToUtcString(Date date) { SimpleDateFormat sdf = getSdf("yyyy-MM-dd'T'HH:mm:ss'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); return sdf.format(date); } /** * UTC 时间字符串转本地时间 * * @param date * @param pattern * @return */ public static Date utcStringToLocalDate(String date, String pattern) { SimpleDateFormat sdf = getSdf(pattern); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date localDate = null; try { localDate = sdf.parse(date); } catch (Exception e) { } return localDate; }
时间格式化及完整类
https://blog.csdn.net/Lxinccode/article/details/79414292解决 无用评论 打赏 举报