Reputation: 1335
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.
Upvotes: 21
Views: 46093
Reputation: 1
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 "";
}
Upvotes: 0
Reputation: 1112
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";
}
}
Upvotes: 63
Reputation: 67502
String S = "1350574775";
You are sending your timestamp in seconds, instead of milliseconds.
Do this, instead:
String S = "1350574775000";
Or, in your getDate
method, multiply by 1000L
:
new Date(Long.parseLong(timeStampStr) * 1000L)
Upvotes: 6