我只想获取 Android 当前时间并传递给 .NET WebAPI。这就对了。但是有很多方法...
我试过这个
安卓端
String TimeOfRequest = Long.toString(new Date().getTime());
.NET 端
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
t = epoch.AddMilliseconds(Convert.ToInt64(locationItem.TimeOfRequest));
但它给出了这个结果
0001-01-02 18:01:34.0366112
请建议最简单的方法。
最佳答案
试试这个:
private String formatDate(long milliseconds) /* This is your topStory.getTime()*1000 */ {
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliseconds);
TimeZone tz = TimeZone.getDefault();
sdf.setTimeZone(tz);
return sdf.format(calendar.getTime());
}
关于android - 将 DateTime 传输到 .NET WebAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47748096/