时间戳记到字符串日期 [英] Timestamp to string date

查看:61
本文介绍了时间戳记到字符串日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将时间戳转换为日期。
我有:

I don't know how to transform timestamp to date. I have:

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";
   }
} 

答案是:1970年1月16日,但是

And answer is: 01/16/1970, but it is wrong.

推荐答案

如果坚持使用 1350574775格式(以秒为单位),请尝试以下操作:

try this if you're sticking with "1350574775" format which is in seconds:

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";
    }
} 

这篇关于时间戳记到字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆