Unm*_*ful 20 android timestamp date
我不知道如何将时间戳转换为日期.我有:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView czas = (TextView)findViewById(R.id.textView1);
String S = "1350574775";
czas.setText(getDate(S));
}
private String getDate(String timeStampStr){
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(timeStampStr)));
return sdf.format(netDate);
} catch (Exception ignored) {
return "xx";
}
}
Run Code Online (Sandbox Code Playgroud)
答案是:1970年1月16日,但是错了.
Pin*_*der 60
如果您坚持使用"1350574775"格式(以秒为单位),请尝试此操作:
private void onCreate(Bundle bundle){
....
String S = "1350574775";
//convert unix epoch timestamp (seconds) to milliseconds
long timestamp = Long.parseLong(s) * 1000L;
czas.setText(getDate(timestamp ));
}
private String getDate(long timeStamp){
try{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(timeStamp));
return sdf.format(netDate);
}
catch(Exception ex){
return "xx";
}
}
Run Code Online (Sandbox Code Playgroud)
String S = "1350574775";
Run Code Online (Sandbox Code Playgroud)
您将以秒为单位发送时间戳,而不是毫秒.
这样做,而不是:
String S = "1350574775000";
Run Code Online (Sandbox Code Playgroud)
或者,在您的getDate
方法中,乘以1000L
:
new Date(Long.parseLong(timeStampStr) * 1000L)
Run Code Online (Sandbox Code Playgroud)