需要帮助在Java中转换UTC日期字符串

时间:2022-04-17 02:03:41

I have a game client an Google App Engine server. My server sends the client a UTC timedate string which is milliseconds since epoch that represents the datestamp of the last play. I want my client to report how many days/hours/min since the last play. I have this working in my iOS client but can't get it to work in Android. I've tried many options, some using Joda-Time some Date objects that are straight Java, the time is always offset by a couple hours. What's odd is the time is off not on the hour.. could be off 2 hours 34 min.. so I don't think my issue is just a timezone thing or it would be on the hour no?

我有一个游戏客户端和一个Google App Engine服务器。我的服务器向客户端发送UTC时间日期字符串,该字符串是自纪元以来的毫秒,表示最后一次播放的日期戳。我希望我的客户报告自上次比赛以来的天/小时/分钟。我在iOS客户端工作但无法在Android中使用它。我尝试了很多选项,有些使用Joda-Time一些直接Java的Date对象,时间总是偏移几个小时。奇怪的是,时间不是时间关闭..可能会关闭2小时34分钟..所以我不认为我的问题只是一个时区的事情,或者它会在一小时没有?

Essentially I need to get this UTC time and then get the current time in UTC as well.. compare the difference to get days/hours/min

基本上我需要获得UTC时间,然后以UTC格式获取当前时间。比较差异以获得天/小时/分钟

Here's what I have, I'm using the Joda-Time library:

这就是我所拥有的,我正在使用Joda-Time库:

For example my server sends me this string "1392392591.0" (happens to be utc time for approx 7:45am PST)

例如我的服务器发送给我这个字符串“1392392591.0”(恰好是太平洋标准时间上午7:45左右的时间)

public String convertDateString ( String date ) {

        Float gameEpoch = Float.parseFloat( date ); 

        DateTime now = new DateTime();
        DateTime gameTime = new DateTime(gameEpoch.longValue() * 1000 );

        Period p = new Period(gameTime, now, PeriodType.dayTime());

        String dateString = "";
        if(p.getDays() > 0)
            dateString =  (p.getDays() + " days " + p.getHours() + " hours ago");
        else if(p.getHours() > 0)
            dateString =  (p.getHours() + " hours " + p.getMinutes() + " minutes ago");
        else if(p.getMinutes() > 0)
            dateString =  (p.getMinutes() + " minutes ago");
        else
            dateString = "Just Now";

        return dateString;
    }

2 个解决方案

#1


5  

Don't convert from a String to a Float. Floats only have 7 significant digits, hence gameEpoch will always be imprecise. Use a double, or better yet, a long.

不要从String转换为Float。浮点数只有7位有效数字,因此gameEpoch总是不精确。使用双倍,或更好,但长。

#2


0  

The answer by Tony the Pony is correct.

Tony the Pony的回答是正确的。

By the way, in the last half of your example code, you are working too hard. Joda-Time offers a PeriodFormatterBuilder class to help you generate those descriptive strings.

顺便说一下,在你的示例代码的后半部分,你工作太辛苦了。 Joda-Time提供PeriodFormatterBuilder类来帮助您生成这些描述性字符串。

This example code needs some finessing, but will get you headed in the right direction.

这个示例代码需要一些finessing,但会让你朝着正确的方向前进。

// Period(int years, int months, int weeks, int days, int hours, int minutes, int seconds, int millis)
Period period = new Period( 0, 0, 0, 2, 3, 4, 0, 0 );

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .printZeroAlways()
        .appendYears()
        .appendSuffix( " year", " years" )
        .appendSeparator( ", " )
        .printZeroRarelyLast()
        .appendMonths()
        .appendSuffix( " month", " months" )
        .appendSeparator( ", " )
        .appendWeeks()
        .appendSuffix( " week", " weeks" )
        .appendSeparator( ", " )
        .appendDays()
        .appendSuffix( " day", " days" )
        .appendSeparator( ", " )
        .appendHours()
        .appendSuffix( " hour", " hours" )
        .appendSeparator( ", and " )
        .appendMinutes()
        .appendSuffix( " minute", " minutes" )
        .toFormatter();

String timeSpanDescription = formatter.print( period );

Dump to console…

转储到控制台......

System.out.println( "period: " + period );
System.out.println( "timeSpanDescription: " + timeSpanDescription );

When run…

跑的时候......

period: P2DT3H4M
timeSpanDescription: 0 years, 2 days, 3 hours, and 4 minutes

#1


5  

Don't convert from a String to a Float. Floats only have 7 significant digits, hence gameEpoch will always be imprecise. Use a double, or better yet, a long.

不要从String转换为Float。浮点数只有7位有效数字,因此gameEpoch总是不精确。使用双倍,或更好,但长。

#2


0  

The answer by Tony the Pony is correct.

Tony the Pony的回答是正确的。

By the way, in the last half of your example code, you are working too hard. Joda-Time offers a PeriodFormatterBuilder class to help you generate those descriptive strings.

顺便说一下,在你的示例代码的后半部分,你工作太辛苦了。 Joda-Time提供PeriodFormatterBuilder类来帮助您生成这些描述性字符串。

This example code needs some finessing, but will get you headed in the right direction.

这个示例代码需要一些finessing,但会让你朝着正确的方向前进。

// Period(int years, int months, int weeks, int days, int hours, int minutes, int seconds, int millis)
Period period = new Period( 0, 0, 0, 2, 3, 4, 0, 0 );

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .printZeroAlways()
        .appendYears()
        .appendSuffix( " year", " years" )
        .appendSeparator( ", " )
        .printZeroRarelyLast()
        .appendMonths()
        .appendSuffix( " month", " months" )
        .appendSeparator( ", " )
        .appendWeeks()
        .appendSuffix( " week", " weeks" )
        .appendSeparator( ", " )
        .appendDays()
        .appendSuffix( " day", " days" )
        .appendSeparator( ", " )
        .appendHours()
        .appendSuffix( " hour", " hours" )
        .appendSeparator( ", and " )
        .appendMinutes()
        .appendSuffix( " minute", " minutes" )
        .toFormatter();

String timeSpanDescription = formatter.print( period );

Dump to console…

转储到控制台......

System.out.println( "period: " + period );
System.out.println( "timeSpanDescription: " + timeSpanDescription );

When run…

跑的时候......

period: P2DT3H4M
timeSpanDescription: 0 years, 2 days, 3 hours, and 4 minutes