根据UTC格式转换的日期和时间串 [英] Convert a date and time string according to UTC format

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

问题描述

我有一个日期字符串 2012年11月21日13时11分25秒这是我从本地数据库中获取。我要转换此根据 UTC 设置和特定的显示在屏幕上。所以,如果它的 GMT + 05:30 应显示为 2012年11月21日18点41分25秒上屏幕。我怎么能做到这一点的转换。我已经查了一些问题,但没有工作了。

I am having a date string 2012-11-21 13:11:25 which I get from local database. I have to convert this according to UTC settings and display it on a particular screen. So if its GMT+05:30 it should be displayed as 2012-11-21 18:41:25 on the screen. How can I do this conversion. I have checked some of the questions but that didn't work out.

我能得到一个Date对象返回类似周三11月21日13时十一分25秒GMT + 05:在这之后30 2012 我需要得到时间为 18时41分25秒及日期 2012年11月21日

I am able to get a Date object that returns something like Wed Nov 21 13:11:25 GMT+05:30 2012 after this I need to get the time as 18:41:25 and date as 11-21-2012

在此先感谢

推荐答案

东风 inputFmt 必须使用相同的格式。

Your df and inputFmt must use the same format.

不过,我认为你应该做的是这样的:

But I think you should do it like this:

    Date myDate = new Date();

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.setTime(myDate);
    Date time = calendar.getTime();
    SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
    String dateAsString = outputFmt.format(time);
    System.out.println(dateAsString);

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

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