Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert timestamp to date in Android?

Tags:

android

I am implementing one android application in which I want to convert timestamp to date but I could not get success. I have tried below things. Please check it if I am doing something wrong.

I am passing this value :

myTimestamp=1328015914;
DateFormat.getDateFormat(mContext).format(new Date(myTimestamp * 1000));

It returns 11/1/1970 but its wrong.

Please help me for this query.

Thanks Manthan Dalal

like image 950
ManthanDalal Avatar asked Oct 08 '22 20:10

ManthanDalal


1 Answers

You might need to specify the time zone. This is the method i use.

private String getDate(long timeStamp){
    DateFormat objFormatter = new SimpleDateFormat("dd-MM-yyyy");
    objFormatter.setTimeZone(TimeZone.getTimeZone(timezone));

    Calendar objCalendar =    
            Calendar.getInstance(TimeZone.getTimeZone(timezone));
    objCalendar.setTimeInMillis(timeStamp*1000);//edit
    String result = objFormatter.format(objCalendar.getTime());
    objCalendar.clear();
    return result;         
}
like image 164
blessanm86 Avatar answered Oct 12 '22 10:10

blessanm86