Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp to string date

Tags:

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

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

like image 875
Unmerciful Avatar asked Nov 05 '12 22:11

Unmerciful


People also ask

How do I convert timestamp to date?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).

How do I convert a timestamp to a date in Python?

Import the “datetime” file to start timestamp conversion into a date. Create an object and initialize the value of the timestamp. Use the ” fromtimestamp ()” method to place either data or object. Print the date after conversion of the timestamp.


1 Answers

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";     } }  
like image 186
PinoyCoder Avatar answered Sep 30 '22 16:09

PinoyCoder