16

我在 StackOverflow Android get Current UTC timeHow can I get the current date and time in UTC or GMT in Java?

我尝试了两种方法来获取我的手机在 GMT 中的当前时间。我在西班牙,时差是 GMT+2。让我们看一个例子: 1º attemp: 我创建了一个格式并将它应用到 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;

它工作正常。

有任何想法吗?

非常感谢,

大卫。

4

6 回答 6

26

这肯定有效!

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

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

于 2012-04-17T06:37:02.483 回答
5
     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);

看看这是否有效。

于 2011-05-16T08:56:18.057 回答
5

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

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

}

焦拉

于 2011-05-31T07:07:26.510 回答
4

你总是可以使用:

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

或者

Calendar mCalendar = Calendar.getInstance(TimeZone.getTimeZone("utc"));
long millies = mCalendar.getTimeInMillis();
于 2014-07-04T23:35:35.513 回答
1

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

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

工作正常。

于 2016-08-03T09:24:42.703 回答
1

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 用于 JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从org.threeten.bp子包中导入日期和时间类。

链接

于 2020-03-30T18:04:17.487 回答