用Java确定另一个国家的互联网时间 [英] Determine the internet time of another country in Java

查看:20
本文介绍了用Java确定另一个国家的互联网时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个可供来自世界各地的人们使用的 Java 应用程序.一项功能要求它显示澳大利亚墨尔本的当前时间.

I am developing a Java application that will be used by people from around the world. One feature requires it to display the current time in Melbourne, Australia.

我发现了这个 回答并调整代码如下,但它返回我的当前时间(如预期).它使用 Apache Commons Net 库:

I have found this answer and adapted the code as follows, but it returns my current time (as expected). It uses the Apache Commons Net library:

    try {
        String TIME_SERVER = "time-a.nist.gov";   
        NTPUDPClient timeClient = new NTPUDPClient();
        InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
        TimeInfo timeInfo = timeClient.getTime(inetAddress);
        long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
        return new Date(returnTime);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return null;
    }

如何修改此代码以返回墨尔本的时间,而不是我的时间?我也愿意接受其他解决方案来解决这个问题.

谢谢!

根据 Jon 的建议,我使用了 JodaTime 库 并构建了以下代码来解决这个问题.通过将澳大利亚/墨尔本更改为此处找到的任何时区,它可以用于其他时区.

Taking Jon's advice, I have used the JodaTime library and built the following code to solve the problem. It can be used for other time zones by changing Australia/Melbourne to any timezone found here.

    try {
        //Get the time for the current time zone.
        String TIME_SERVER = "time-a.nist.gov";   
        NTPUDPClient timeClient = new NTPUDPClient();
        InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
        TimeInfo timeInfo = timeClient.getTime(inetAddress);
        long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();

        //Format it to the Melbourne TimeZone.
        DateTimeZone tzMelbourne = DateTimeZone.forID("Australia/Melbourne");
        return new DateTime(returnTime).toDateTime(tzMelbourne);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return null;
    }

推荐答案

您当前正在返回一个 java.util.Date - 它没有 时间区.这只是一瞬间.它不在您的时区或其他时区 - 这只是一瞬间.当您调用 toString() 时,它将为您提供本地时区的文本表示,但这不是对象中数据的一部分.

You're currently returning a java.util.Date - that doesn't have a time zone. It's just an instant in time. It's not in your time zone, or some other time zone - it's just an instant. When you call toString(), that will give you a textual representation in your local time zone, but that's not part of the data in the object.

如果您需要表示特定时区中的日期/时间",那么您应该使用 Calendar 类,或者最好使用 Joda Time 这是一个更好的日期/时间 API.

If you need to represent "a date/time in a particular time zone" then you should use the Calendar class, or ideally use Joda Time which is a much better date/time API.

或者,如果您只是想格式化特定时区中的日期/时间,您可以使用 SimpleDateFormat - 设置时区,然后格式化您已经返回的 Date 值.

Alternatively, if you're just trying to format the date/time in a particular time zone, you can use SimpleDateFormat - set the time zone, and then format the Date value you're already returning.

这篇关于用Java确定另一个国家的互联网时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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