我是Joda的新手,需要帮助。我试图将这个C#代码转换为java。我一直在研究Joda-time以及查看库的文档。我正试图获得差异时代和UTC现在()。但不知道如何使用Joda的Interval类来实现这一目标。这是C#方法获取epoch并根据UTC now()进行检查。然后得到两个日期之间的差异。
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));
}
这很好但现在我想在使用Joda的java中做同样的事情。
这是我一直试图做的,但我是要了解如何使用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
答案 0 :(得分:0)
我不是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
似乎只获取自纪元以来的秒数并将其转换为字符串。所以这是等效的Java代码:
String stamp = String.valueOf(System.currentTimeMillis() / 1000);