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.
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).
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.
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"; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With