如何使用 Joda-Time 将 C# DateTime 转换为 Java DateTimes
How covert C# DateTime to Java DateTimeusing Joda-Time
我是 Joda 的新手,需要帮助。我正在尝试将此 C# 代码转换为 java。我一直在研究 Joda-time 以及查看图书馆的文档。我正在尝试区分纪元和 UTC now()。但不确定如何使用 Joda 的间隔 class 实现此目的。这是获取纪元并根据 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 做同样的事情。
这是我一直在尝试做的事情,但我是否要了解如何使用间隔 class.
来了解两次之间的差异
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
似乎只是获取自纪元以来的秒数并将其转换为字符串。所以这是等效的 Java 代码:
String stamp = String.valueOf(System.currentTimeMillis() / 1000);
我是 Joda 的新手,需要帮助。我正在尝试将此 C# 代码转换为 java。我一直在研究 Joda-time 以及查看图书馆的文档。我正在尝试区分纪元和 UTC now()。但不确定如何使用 Joda 的间隔 class 实现此目的。这是获取纪元并根据 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 做同样的事情。
这是我一直在尝试做的事情,但我是否要了解如何使用间隔 class.
来了解两次之间的差异 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
似乎只是获取自纪元以来的秒数并将其转换为字符串。所以这是等效的 Java 代码:
String stamp = String.valueOf(System.currentTimeMillis() / 1000);