21

タイムスタンプを日付に変換する方法がわかりません。私は持っています:

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日ですが、それは間違っています。

4

3 に答える 3

63

秒単位の「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";
    }
} 
于 2012-11-06T05:22:48.020 に答える
6
String S = "1350574775";

タイムスタンプをミリ秒ではなく秒単位で送信しています。

代わりに、次のようにします。

String S = "1350574775000";

または、あなたのgetDate方法では、次のように掛け1000Lます。

new Date(Long.parseLong(timeStampStr) * 1000L)
于 2012-11-05T22:15:22.807 に答える
0
 public static String getTime(long timeStamp){
    try{
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
        Date date = (Date) calendar.getTime();
        return sdf.format(date);
    }catch (Exception e) {
    }
    return "";
}
于 2019-08-05T08:52:06.810 に答える