将给定时间转换为 GMT

发布于 2024-11-25 05:11:00 字数 665 浏览 0 评论 0原文

我曾尝试将给定日期 Mon Jul 04 00:00:00 IST 2011 转换为 GMT,如下所示:2011-07-04 18:10:47 GMT+00:00 2011< /code> 但它显示 3/7/11 6:30 PM

这是我的代码:

java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java.text.SimpleDateFormat res_format = new SimpleDateFormat("dd/mm/yyyy HH:mm");

java.util.Date date1 = format.parse("2011-07-04 00:00:00");

DateFormat gmtFormat = new SimpleDateFormat();

TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");

gmtFormat.setTimeZone(gmtTime);

System.out.println("Current Time: "+date1);

System.out.println("Time:"+gmtFormat.format(date1));

I had tried to convert the given date Mon Jul 04 00:00:00 IST 2011 to GMT like this: 2011-07-04 18:10:47 GMT+00:00 2011 but it displays 3/7/11 6:30 PM

This is my code:

java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

java.text.SimpleDateFormat res_format = new SimpleDateFormat("dd/mm/yyyy HH:mm");

java.util.Date date1 = format.parse("2011-07-04 00:00:00");

DateFormat gmtFormat = new SimpleDateFormat();

TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");

gmtFormat.setTimeZone(gmtTime);

System.out.println("Current Time: "+date1);

System.out.println("Time:"+gmtFormat.format(date1));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意犹 2024-12-02 05:11:00

这对我有用:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class TZ {

    public static void main(String[] args) throws ParseException {
    java.text.SimpleDateFormat sourceFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");

    java.text.SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss zzzz");

    java.util.Date date1 = sourceFormat.parse("Mon Jul 04 00:00:00 IST 2011");

    TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");

    gmtFormat.setTimeZone(gmtTime);

    System.out.println("Source date: " + date1);

    System.out.println("gmt:" + gmtFormat.format(date1));

    }
}

This works for me:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class TZ {

    public static void main(String[] args) throws ParseException {
    java.text.SimpleDateFormat sourceFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");

    java.text.SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss zzzz");

    java.util.Date date1 = sourceFormat.parse("Mon Jul 04 00:00:00 IST 2011");

    TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");

    gmtFormat.setTimeZone(gmtTime);

    System.out.println("Source date: " + date1);

    System.out.println("gmt:" + gmtFormat.format(date1));

    }
}
兔姬 2024-12-02 05:11:00

对代码进行最小更改的快速解决方案是将: 替换

DateFormat gmtFormat = new SimpleDateFormat();

为:

SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");

Quick solution with minimal change in your code is to replace:

DateFormat gmtFormat = new SimpleDateFormat();

To:

SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
云雾 2024-12-02 05:11:00

你的想法是正确的。如果您费心再次查看代码,您就会知道自己做错了什么。

你的代码工作正常。但是您没有看到您想要的内容,因为您(很可能)已经

java.text.SimpleDateFormat res_format = new SimpleDateFormat("dd/mm/yyyy HH:mm");

对输出

进行了初始化,并且您使用全新的 SimpleDateFormat,突然设置时区和显示。

如果您更改

DateFormat gmtFormat = new SimpleDateFormat();

java.text.SimpleDateFormat gmtFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm zz");

您会得到您想要的。

(还要注意格式的变化。它是 dd/MM/yyyy 而不是 dd/mm/yyyy

您的代码会更容易阅读(对于您和其他人)如果您已对块进行了逻辑分组,并且没有对(几乎)所有类使用完全限定名称

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = format.parse("2011-07-04 00:00:00");

    SimpleDateFormat gmtFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm zz");
    TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");
    gmtFormat.setTimeZone(gmtTime);

    System.out.println("Current Time: " + date);

    System.out.println("Time:" + gmtFormat.format(date));

Your idea is correct. If you had bothered to look into the code once more, you would have known what you have done wrong.

Your code is working fine. But you don't see what you want because you have (most likely) initialized

java.text.SimpleDateFormat res_format = new SimpleDateFormat("dd/mm/yyyy HH:mm");

for the output

and you use a brand new SimpleDateFormat, out of the blue to set the timezone and display.

If you change

DateFormat gmtFormat = new SimpleDateFormat();

to

java.text.SimpleDateFormat gmtFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm zz");

You get what you want.

(Note the change in the format as well. It is dd/MM/yyyy and not dd/mm/yyyy)

You code would have been a lot more easier to read (for you, and others) if you had logically grouped the blocks, and not used fully qualified names for (almost) all classes

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = format.parse("2011-07-04 00:00:00");

    SimpleDateFormat gmtFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm zz");
    TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");
    gmtFormat.setTimeZone(gmtTime);

    System.out.println("Current Time: " + date);

    System.out.println("Time:" + gmtFormat.format(date));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文