我有这样的字符串2013-10-22T01:37:56.我需要将此字符串更改为UTC日期格式,如MM / dd / yyyy KK:mm:ss a.我已经尝试了一些代码,但它没有返回UTC datetime.

我的代码是

String[] time = itsAlarmDttm.split("T");
        String aFormatDate = time[0]+ " "+time[1];
        String aRevisedDate = null;
        try {
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            final Date dateObj = sdf.parse(aFormatDate);
            aRevisedDate = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a").format(dateObj);
            System.out.println(aRevisedDate);
        } catch (ParseException e) {
            itsLogger.error("Error occured in Parsing the Data Time Object:  " +e.getMessage());
        } catch (Exception e) {
            itsLogger.error("Error occured in Data Time Objecct:  " +e.getMessage());
        }

我得到的输出是MM / dd / yyyy KK:mm:ss a格式.但不是UTC时间格式.
如何解决这个问题?

解决方法

尝试使用Z或z时区标记格式化日期:

new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a Z").format(dateObj);

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。