java日期解析异常,同时将UTC传送到本地时区 [英] java date parse exception while conveting UTC to local time zone

查看:78
本文介绍了java日期解析异常,同时将UTC传送到本地时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下格式的UTC日期以及如何将此UTC日期转换为我当地的时区?

I get a UTC date with following format and how can convert this UTC date to my local time zone?

我的输入日期是 2015 -03-17 06:00:00 +0000 我尝试了以下编码,它给出了解析异常。

my input date is 2015-03-17 06:00:00 +0000 and I tried the following coding its gives parsing exception.

我的代码:

DateFormat utcFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss 'Z'");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = utcFormat.parse("2015-03-17 06:00:00 +0000"); // java.text.DateFormat.parse(Unknown Source)
utcFormat.setTimeZone(TimeZone.getDefault());
System.out.println(utcFormat.format(date));


推荐答案

您的代码中有两个错误。

You have two mistakes in your code.


  1. 正如其他人已经提到的那样,你的模式中还有' Z

  2. 您将模式指定为 dd-MM-yyyy 但尝试将日期解析为 yyyy-MM-dd

  1. As others already mentioned you have additional ' in your pattern around Z.
  2. You specify the pattern as dd-MM-yyyy but try to parse the date as yyyy-MM-dd.

正确版本:

DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = utcFormat.parse("2015-03-17 06:00:00 +0000");
utcFormat.setTimeZone(TimeZone.getDefault());
System.out.println(utcFormat.format(date));

这篇关于java日期解析异常,同时将UTC传送到本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆