使用 Android 获取 GMT 时间

发布于 2024-11-07 07:16:02 字数 1570 浏览 2 评论 0原文

我在 StackOverflow 上研究这个问题有一段时间了 Android 获取当前 UTC 时间如何我在 Java 中获取当前的 UTC 或 GMT 日期和时间?

我尝试了两种方法来获取手机的当前 GMT 时间。我在西班牙,时差是 GMT+2。让我们看一个例子: 1° 尝试:我创建了一种格式并将其应用于 System.currentTimeMillis();

    DateFormat dfgmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
    dfgmt.setTimeZone(TimeZone.getTimeZone("GMT")); 
    String gmtTime = dfgmt.format(new Date());
    //Using System.currentTimeMillis() is the same as new Date()
    Date dPhoneTime = dfgmt.parse(gmtTime);
    Long phoneTimeUTC = dPhoneTime.getTime();

我需要将这个时间减去另一个时间,这就是我将演员转换为 Long 的原因。

    DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");          
    Date arrivalDate = df.parse(item.getArrivalDate());
    //the String comes from JSON and is for example:"UTC_arrival":"2011-05-16 18:00:00"
    //which already is in UTC format. So the DateFormat doesnt have the GMT paramater as dfgmt
    diff = arrival.getTime() - phoneTimeUTC ;

我也尝试过这个:

    Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()

但我仍然没有得到正确的区别。但如果我这样做:

    Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()-3600000*2;

它确实可以正常工作。

有什么想法吗?

非常感谢,

大卫。

I have been digging into the question for a while in StackOverflow
Android get Current UTC time
and
How can I get the current date and time in UTC or GMT in Java?

I have tried two ways to get the current time of my phone in GMT. I am in Spain and the difference is GMT+2. So let's see with an example:
1º attemp: I created a format and applied it to System.currentTimeMillis();

    DateFormat dfgmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
    dfgmt.setTimeZone(TimeZone.getTimeZone("GMT")); 
    String gmtTime = dfgmt.format(new Date());
    //Using System.currentTimeMillis() is the same as new Date()
    Date dPhoneTime = dfgmt.parse(gmtTime);
    Long phoneTimeUTC = dPhoneTime.getTime();

I need to substract that time to another time, that's why i do the cast to Long.

    DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");          
    Date arrivalDate = df.parse(item.getArrivalDate());
    //the String comes from JSON and is for example:"UTC_arrival":"2011-05-16 18:00:00"
    //which already is in UTC format. So the DateFormat doesnt have the GMT paramater as dfgmt
    diff = arrival.getTime() - phoneTimeUTC ;

I also tried this:

    Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()

And still I dont get the right difference. But if I do this:

    Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()-3600000*2;

It does work OK.

Any ideas?

Thanks a lot,

David.

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

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

发布评论

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

评论(6

伊面 2024-11-14 07:16:02

这肯定有效!

        SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(dateFormatGmt.format(new Date())+"");

指定格式,您将在 GMT 中得到它!

This works for sure!

        SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        System.out.println(dateFormatGmt.format(new Date())+"");

Specify the format, and you will get it in GMT!

走过海棠暮 2024-11-14 07:16:02

据我阅读 calendar.getTimeInMillis();返回以毫秒为单位的 UTC 时间。我使用了以下代码,并将其与本网站 http://www.xav.com/time 中的纪元进行了比较.cgi

public int GetUnixTime()
{
    Calendar calendar = Calendar.getInstance();
    long now = calendar.getTimeInMillis();
    int utc = (int)(now / 1000);
    return (utc);

}

乔拉

As far as I read the calendar.getTimeInMillis(); returns the UTC time in millis. I used the following code and compared it to the Epoch in this site http://www.xav.com/time.cgi.

public int GetUnixTime()
{
    Calendar calendar = Calendar.getInstance();
    long now = calendar.getTimeInMillis();
    int utc = (int)(now / 1000);
    return (utc);

}

Giora

心如狂蝶 2024-11-14 07:16:02
     Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
     Date currentLocalTime = cal.getTime();
     DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");   
     date.setTimeZone(TimeZone.getTimeZone("GMT")); 
     String localTime = date.format(currentLocalTime); 
     System.out.println(localTime);

看看是否有效。

     Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
     Date currentLocalTime = cal.getTime();
     DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");   
     date.setTimeZone(TimeZone.getTimeZone("GMT")); 
     String localTime = date.format(currentLocalTime); 
     System.out.println(localTime);

Have a look and see if that works.

心清如水 2024-11-14 07:16:02

您始终可以使用:

Calendar mCalendar = Calendar.getInstance(TimeZone.getTimeZone("gmt"));
long millies = mCalendar.getTimeInMillis();

Calendar mCalendar = Calendar.getInstance(TimeZone.getTimeZone("utc"));
long millies = mCalendar.getTimeInMillis();

you can always use:

Calendar mCalendar = Calendar.getInstance(TimeZone.getTimeZone("gmt"));
long millies = mCalendar.getTimeInMillis();

or

Calendar mCalendar = Calendar.getInstance(TimeZone.getTimeZone("utc"));
long millies = mCalendar.getTimeInMillis();
心房敞 2024-11-14 07:16:02

输出:2016-08-01 14:37:48 UTC

    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

工作正常。

Output: 2016-08-01 14:37:48 UTC

    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

Works fine.

抹茶夏天i‖ 2024-11-14 07:16:02

java.time 和 ThreeTenABP

我提供现代答案。

要获取现在(在西班牙或任何其他地方)的电话时间与过去某个时间之间的毫秒差异:

    // Example arrival time for the demonstration
    Instant arrival = Instant.parse("2020-02-29T12:34:56.789Z");
    Instant currentTime = Instant.now();
    long difference = ChronoUnit.MILLIS.between(arrival, currentTime);
    System.out.println("Difference is " + difference + " milliseconds");

示例输出:

差异为 2610350731 毫秒

如果您想要以秒或其他时间单位为单位的差异,只需使用 ChronoUnit 枚举中的适当枚举常量而不是 ChronoUnit.MILLIS 即可。

无需担心设备时区,也无需担心格式化或解析时间,这些担心只会导致这个基本简单的事情变得过于复杂。

顺便说一句,纪元是一个明确定义的时间点,它不随时区而变化,在世界各地都是相同的。因此,从纪元到现在的毫秒数在所有时区也是相同的。有人说此计数始终采用 UTC 格式,因为纪元(通常)是用 UTC 定义的,如 1970 年 1 月 1 日 00:00 UTC。

问题:java.time 不需要 Android API 级别 26 吗?

java.time 在旧版和新版 Android 设备上都能很好地工作。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及较新的 Android 设备(从 API 级别 26 开始)中,现代 API 是内置的。
  • 在非 Android Java 6 和 7 中,获取 ThreeTen Backport,即现代类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保使用子包从 org. Threeten.bp 导入日期和时间类。

链接

java.time and ThreeTenABP

I am providing the modern answer.

To get the difference in milliseconds between the phone time now — in Spain or any other place — and a certain time in the past:

    // Example arrival time for the demonstration
    Instant arrival = Instant.parse("2020-02-29T12:34:56.789Z");
    Instant currentTime = Instant.now();
    long difference = ChronoUnit.MILLIS.between(arrival, currentTime);
    System.out.println("Difference is " + difference + " milliseconds");

Example output:

Difference is 2610350731 milliseconds

If you want the difference in seconds or some other time unit, just use the appropriate enum constant from the ChronoUnit enum instead of ChronoUnit.MILLIS.

There is no need to worry about the device time zone, nor about formatting or parsing the time, those worries only lead to over-complication of this basically simple matter.

BTW the epoch is one well-defined point in time, it doesn’t vary with time zone, it’s the same all over the world. Therefore the count of milliseconds from the epoch till now is also the same in all time zones. Some say that this count is always in UTC because the epoch is (usually) defined in UTC, as January 1, 1970 at 00:00 UTC.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

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