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解决 无用评论 打赏 举报
悬赏问题
- ¥30 求解达问题(有红包)
- ¥15 请解包一个pak文件
- ¥15 不同系统编译兼容问题
- ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
- ¥30 数字电源对DSP芯片的具体要求
- ¥20 antv g6 折线边如何变为钝角
- ¥30 如何在Matlab或Python中 设置饼图的高度
- ¥15 nginx中的CORS策略应该如何配置
- ¥30 信号与系统实验:采样定理分析
- ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao