如何使用Joda-Time将c# DateTime转换为Java DateTime



我是新来的,需要帮助。我正试图将这个c#代码转换为java。我一直在做一些关于Joda-time的研究,并查看了该库的文档。我正试图得到不同的epoch和UTC现在()。但不确定如何使用Joda的Interval类实现这一点。下面是获取epoch并根据UTC now()检查它的c#方法。然后得到两个日期的差值。

public static void AddApiAuthenticationHeaders(this HttpClient httpClient, HttpMethod httpMethod, string publicKey, string privateKey)
    {
        httpClient.DefaultRequestHeaders.Add("X-PSK", "thisisatestpublickey");//key:K-PSK
        DateTime epochStart = new DateTime(1970,01,01,0,0,0,0, DateTimeKind.Utc); //Getting UTC DATE since epoch
        TimeSpan ts = DateTime.UtcNow - epochStart; //get the current timestamp between now and january 1970
        string stamp = Convert.ToUInt64(ts.TotalSeconds).ToString(); //get the total seconds that have elasped
        httpClient.DefaultRequestHeaders.Add("X-Stamp", stamp);
        //research extension method
        string[] data = new string[] {publicKey, stamp.ToString(), httpMethod.Method };
        byte[] expectedSignature = data.ComputeHash(privateKey);
        httpClient.DefaultRequestHeaders.Add(HttpRequestMessageExtensions.SignatureHeaderName, Convert.ToBase64String(expectedSignature));
    }

这工作得很好,但现在我想在java中使用Joda做同样的事情。

这就是我一直在尝试做的,但我想了解如何使用Interval类获得两个时间的差异。

 DateTime epochStart = new DateTime(1970,1,1,0,0,0,0, DateTimeZone.UTC);
    Interval ts = new Interval(DateTime.now(DateTimeZone.UTC).getMillis(), epochStart.getMillis());
    String stamp = ts.toString(); // I know here is not right

我不是真正的c#专家,但是这段代码:

DateTime epochStart = new DateTime(1970,01,01,0,0,0,0, DateTimeKind.Utc); //Getting UTC DATE since epoch
TimeSpan ts = DateTime.UtcNow - epochStart; //get the current timestamp between now and january 1970
string stamp = Convert.ToUInt64(ts.TotalSeconds).ToString(); //get the total seconds that have elasped

似乎只是获得自epoch以来的秒数并将其转换为字符串。下面是等效的Java代码:

String stamp = String.valueOf(System.currentTimeMillis() / 1000);

相关内容

  • 没有找到相关文章

最新更新