日期到UTC格式Java [英] Date in to UTC format Java

查看:108
本文介绍了日期到UTC格式Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a string like this 2013-10-22T01:37:56. I Need to change this string into UTC Date format like this MM/dd/yyyy KK:mm:ss a. I have tried some code but it is not returning the 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时间格式。
如何解决这个问题?

I am getting the output is MM/dd/yyyy KK:mm:ss a format. But Not UTC time format. How to solve this issue?

推荐答案

尝试使用 Z z 时区标志:

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

这篇关于日期到UTC格式Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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