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

查看:86
本文介绍了用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库,并构建了以下代码来解决这个问题.通过将澳大利亚/墨尔本更改为在此处找到任何时区,可以将其用于其他时区./p>

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天全站免登陆